i try to use selenium webdriver to locate a element:
i want click the "用户查询"
corresponding html:
the red frame
python:
>>> driver.find_element_by_xpath("//div[#id='container']/h1[2]/a").click()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
driver.find_element_by_xpath("//div[#id='container']/h1[2]/a").click()
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
(Session info: chrome=52.0.2743.116)
(Driver info: chromedriver=2.7.236900,platform=Windows NT 6.1 SP1 x86)
how to locate this element?
Try this below xpath with indexing.
Here. [1] indicates index number of your a tag. suppose in your html, there are numbers of a tag available with different names like this. 用户查询. Suppose, This text 用户查询 index number is one inside your html then use below xpath.
//h1[1]/a
If This text 用户查询 index number is two inside your html then use below xpath.
//h1[2]/a
You could try locating the element based on it's link text:
driver.find_element_by_link_text("用户查询").click();
Related
I'm try many method but not work U have any solution?
<button type="button" class="btn btn-solid-primary btn--l _3Kiuzg" aria-disabled="false">buy</button>
My code and Error
browser.find_elements_by_css_selector(".btn-solid-primary").click()
AttributeError: 'list' object has no attribute 'click'
i can try
find_elements_by_class_name
not work
browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")
error
Traceback (most recent call last):
File "D:\Project-Program\selenium\test3.py", line 11, in <module>
browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-solid-primary btn--l _3Kiuzg"}
Here is the ans - Change elements to element in the below code.
browser.find_element_by_css_selector(".btn-solid-primary").click()
Also, I assume, you're parsing Instagram. It uses dynamic classes (In your case, _3Kiuzg), that change after page reload. So ensure, you're not looking for element by it. Function find_element_by_class_name is not taking many classes, so, if you want such, you should use find_element_by_css_selector, and selector .class_1.class_2.etc
I'm making a scraper that will through my webpage and grab all of the links. A lot of the links are in closed list also known as a tree. Therefore, I found the xpath that holds all of the links. I ran the following xpath in google inspect and it ran perfectly fine giving me the following output.
var result=$x("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
result[0].value
"/doku.php?ihome"
result[4].value
"/doku.php?start"
I than transferred the xpath into selenium code:
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
for aa in a:
print(aa)
I then ran the code and received the following error:
opening browser
Login Successful
Traceback (most recent call last):
File "wiki.py", line 49, in <module>
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[#id='index__tree']//a[contains(text(),doku.php)]/#href" is: [object Attr]. It should be an element.
(Session info: headless chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.86,platform=Linux 3.10.0-957.12.2.el7.x86_64 x86_64)
Try replacing
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
for aa in a:
print(aa)
with
a = [elem.get_attribute("href") for elem in driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]")]
for aa in a:
print(aa)
Notice I removed the "/#href" from the end of your selector.
The Selenium selectors must return a WebElement. By specifying the "/#href", it returned the href attribute of that element instead of the element itself.
The method get_attribute(attribute_name) returns the attribute of an element. Then, you can loop through it.
I have the following line in my script code, where the XPath I got it from Selenium IDE that works fine:
driver.find_element_by_xpath("(//a[contains(#href, '')])[20]").click()
An automation test stops here with this error:
Traceback (most recent call last):
File "Script.py", line 65, in <module>
driver.find_element_by_xpath("//a[contains(#href, '')])[20]").click()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)
How to fix this issue?
Thanks for any help.
Seeing as you just want to scrape the data, I recommend you use this solution:
element = driver.find_element_by_xpath("(//a[contains(#href, '')])[20]")
driver.execute_script("arguments[0].click();", element)
Which clicks the element via Javascript as opposed to a "natural" click that selenium uses (to try to simulate the user experience).
I answered a similar question here that links to another post on it as well.
Sometimes you may need to copy Full XPATH. That was one work around I found.
I would like to share my experience on that in case someone else had same scenario.
I received same error message "Message: element not interactable".
And after like two hours of troubleshooting it turned out that there was another hidden element identified by XPATH. So, I modified my XPATH to ensure capturing targeted element only.
I was able to fix this issue with using full Xpath instead of x path for anyone coming here in the future I hope this will help. I think the reason for this is the element I wanted to click was wrapped by another element so it was not interactable
I am using Selenium webdriver with firefox to download real states data from a website. Once the inputs are entered a pop up window will ask to either select 'Cancel' or 'Save File'.
The problem is that I'm not able to find how to 'click' these buttons. I know it is already the current window in my driver because I am able to change the enter image description here position.
If I dodriver.find_element_by_xpath('//*'), this is the error I get:
Traceback (most recent call last):
File "<ipython-input-131-e87e382e3fb9>", line 1, in <module>
driver.find_element_by_xpath('//*')
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 295, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 756, in find_element
'value': value})['value']
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: No such content frame; perhaps the listener was not registered
I've searched on the web for solutions and most of them mention switching to alert, driver.switch_to_alert().accept(). This is the error I'm getting:
Traceback (most recent call last):
File "<ipython-input-142-bd939a2be33d>", line 1, in <module>
driver.switch_to_alert().accept()
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/common/alert.py",
line 81, in accept
self.driver.execute(Command.ACCEPT_ALERT)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: No tab modal was open when attempting to get the dialog text
This issue is very similar to Not able to locate element on a modal pop up window : selenium, but I don't understand how the accepted answer there was able to find the selectors.
Any ideas how to solve this issue would be greatly appreciated.
I have a problem with selenium with python 3.5, after install all the windows updates all my selenium script broken, I receive every time the same error:
Traceback (most recent call last):
File "C:/Users/Carlo/Desktop/CEx/src/IE.py", line 12, in
a=driver.find_element_by_xpath("//*[#id='un']")
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to find element on closed window
But I really don't understand why because the window of IE is open!
This is my code (simple just to check why I can't make it work more):
import time from
selenium import webdriver
driver = webdriver.Ie()
driver.get('http://gala.test-platform.celtrino.com/Login.aspx')
time.sleep(10)
driver.find_element_by_xpath(".//*[#id='un']")
The code fail every time in the last line and I double check with firepath and the xpath is correct so I don't really understand why it's not working.
I knew, IE has problems with xpath. It does not support xpath directly. It needs third party tools to do this. So, I suggest you to try cssSelector or any other options instead. Since, the element has an ID so you could use this. It's better.
driver.find_element_by_id("un");