I have looked at the Selenium documentation and can not find an example of clicking on an element. I inspected an element named attatchments.
Everytime I attempt to click the element Python gives me an error usually one of these:
{'using': by, 'value': value})['value'] File
"C:\Python27\lib\site-packages\selenium-2.24.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",
line 155, in execute
self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium-2.24.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py",
line 147, in check_response
raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: u'Unable to locate element:
{"method":"name","selector":"add individuals"}'
Here is the code I am trying:
driver.find_element_by_name("attatchments")
elem.click()
If you know element id then you can get it as
element = driver.find_element(by=By.ID, value="element_id")
Or
element = driver.find_element_by_id("element_id")
and then use element.click()
You can go through this link to know the methods in Webdriver class: http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html
Related
I have been using a simple Python2.7 script using Selenium with Chromedriver to open a browser and download some files. Lately, I got an error saying that Chromedriver is outdated (Chrome version must be between 70 and 73) while my Chrome was ver. 75. So I downloaded ChromeDriver 75.0.3770.90 which should be OK for my Chrome version, but I got another error saying "selenium.common.exceptions.WebDriverException: Message: invalid argument: unrecognized capability: chromeOptions".
I have found some tips on the Internet saying that a way ChromeOptions should be used had changed, but I am unable to find how these changes exactly should look like in Python code. I'll be grateful for any helpful hint or solution.
def browser(full_url):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\%s\AppData\Local\Temp\\" % getpass.getuser(),
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
options.add_argument("--window-size=640,480")
driver = webdriver.Chrome(chrome_options=options)
try:
driver.get(full_url)
print("\nURL successfully Accessed\n")
time.sleep(9)
finally:
driver.close()
browser(full_url)
Error I got:
Traceback (most recent call last):
File "C:\Users\M\Documents\Skrypty\domains.py", line 59, in <module>
browser(full_url)
File "C:\Users\M\Documents\Skrypty\domains.py", line 46, in browser
driver = webdriver.Chrome(chrome_options=options)
File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: unrecognized capability: chromeOptions
Just me guessing here but I encountered this as well. I used Python 2.7 and Selenium 3.4.1. I couldn't replicate the problem with Python 3.7 and with Selenium 3.14.0. So the changes lie probably in the changed Selenium. You should look into that.
EDIT: Yeah I just updated Selenium (to 3.14.0) and the problem went away. Now I got the driver working with both Python 2.7 and 3.7
Selenium searches for the xpath to be clicked in the background and gives error. Actual element to be clicked is in a box. Please see the image. I want to click on interface (22) tab. But the script throws the following error.
Traceback (most recent call last):
File "C:/Python27/Lib/site-packages/taut_mcp/communication/mcp_web.py", line 193, in <module>
mcp.interface_data_get()
File "C:\Python27\Lib\site-packages\taut_mcp\command\mcp_gui.py", line 128, in interface_data_get
self.force_click('//div[4]/div/div')
File "C:\Python27\Lib\site-packages\taut_selenium\taut_selenium.py", line 844, in force_click
found_element = self.element_find(element)
File "C:\Python27\Lib\site-packages\taut_selenium\taut_selenium.py", line 296, in element_find
found_element = self.any_condition_wait(element, real_timeout)
File "C:\Python27\Lib\site-packages\taut_selenium\taut_selenium.py", line 820, in any_condition_wait
found_element = this_wait.until(ec((by_id, this_selector)))
File "C:\Python27\Lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/uvijayac/appdata/local/temp/tmp3wnxmc/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///c:/users/uvijayac/appdata/local/temp/tmp3wnxmc/extensions/fxdriver#googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/uvijayac/appdata/local/temp/tmp3wnxmc/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/uvijayac/appdata/local/temp/tmp3wnxmc/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/uvijayac/appdata/local/temp/tmp3wnxmc/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
I'm trying to loop through the list of companies in the Link. The link of each company name is dynamic for example http://ae.bizdirlib.com/node/946273 - Text link 946273 keeps changing i.e its dynamic. I want open each of these links in the page in a browser I'm really confused on how to do this. I have tried this for now.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
#wait until the pages are loaded
browser.implicitly_wait(3)
browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
browser.refresh()
page_source = browser.page_source
for node in page_source:
link = browser.find_element_by_link_text('node').click
On executing this code it gives a error
Traceback (most recent call last):
File "C:/Python27/automation scripts/ggulf/large data.py", line 29, in <module>
link = browser.find_element_by_link_text('node').click
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 276, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 684, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"link text","selector":"node"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/akrakhan/appdata/local/temp/tmppveyk8/extensions/fxdriver#googlecode.com/components/driver-component.js:10299)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/akrakhan/appdata/local/temp/tmppveyk8/extensions/fxdriver#googlecode.com/components/driver-component.js:603)
You are better off looking for something more specific rather than looking through the page source. All the company links are links inside a H2 tag. You can find them using a CSS selector h2 > a which is read find all A tags that are a child of (>) an h2 element.
browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")
for link in links:
link.click
This isn't the final solution because clicking the link will take you off the main page but it's a parallel to what you were trying to accomplish. Probably a better approach would be to store the URLs of all the company links in a string array and then loop through that array navigating to each URL... or something like that. An exercise for the reader... :)
Im still trying to make a social network with py2neo+flask+neo4j.
I've got a problem while searching my database with py2neo.I wanna find all the users that their username includes a special string.For example all the users that their username includes "dav".I wrote the code below and i dont know why i get this error...
from py2neo import Graph
graph=Graph("http://neo4j:123#localhost:7474/ ")
def search(name):
users=graph.merge("Person")
for N in users:
print N['username']
and this is my error:
Traceback (most recent call last):
File "", line 1, in
File "/home/ali/Desktop/flask/search.py", line 10, in search users=graph.cypher.execute('match (p:Person) return p'
File "/usr/local/lib/python2.7/dist-packages/py2neo/core.py", line 659, in cypher metadata = self.resource.metadata
File "/usr/local/lib/python2.7/dist-packages/py2neo/core.py", line 213, in metadata self.get()
File "/usr/local/lib/python2.7/dist-packages/py2neo/core.py", line 267, in get raise_from(self.error_class(message, **content), error)
File "/usr/local/lib/python2.7/dist-packages/py2neo/util.py", line 235, in raise_from raise exception py2neo.error.GraphError: HTTP GET returned response 404
Your URL is wrong, you should change it to this:
Graph("http://neo4j:123#localhost:7474/db/data")
Also, you can't execute cypher through the merge function, instead you should do this:
users = graph.cypher.execute('match (p:Person) return p')
I have HTML that looks like the three following sample statements:
...
12
13
(I'd presently be on pg. 11.)
I don't know the Py/Selenium/Splinter syntax for selecting one of the page numbers in a list and clicking on it to go to that page. (Also, I need to be able to identify the element in the argument as, for example, 'Page$10' or 'Page$12', as seen in the __doPostBack notation. Maybe just a 'next page', in so many words, would be fine, but I don't even know how to do that.)
Thank you for any help.
UPDATE II: Here's the code I have to work from:
import time
import win32ui
import win32api
import win32con
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from ctypes import *
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://[site]');
UPDATE III:
Traceback (most recent call last):
File "montpa_05.py", line 47, in <module>
continue_link = driver.find_element_by_link_text('4')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 246, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 680, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 165, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n
(Session info: chrome=28.0.1500.95)\n (Driver info: chromedriver=2.2,platform=
Windows NT 6.1 SP1 x86_64)'
The <a> element is defined as a link. That means that you can select it by link text.
I don't know Python, but the java syntax would be By.linkText(##) where ## is the number you want to click on.