How to fix this issue element not interactable Selenium Python - python

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

Related

have a special web page can't find any elements

The website is https://www.gaitame.com/bo/demo.html
in middle have a button with japanese "デモトレード版を利用する"
then will be go to a demo trader page like this
in this page i cant get any elements
other page is all right
i already switch_to this tab
please help.............
here is my testing code
from selenium import webdriver
import time
url = 'https://www.gaitame.com/bo/demo.html'
driver = webdriver.Chrome()
driver.get(url)
# Click the button and wait for loading
driver.find_element_by_class_name("blue2btn").click()
time.sleep(5)
# Switch to another tab
driver.switch_to.window(driver.window_handles[1])
# Try find any elements
driver.find_elements_by_class_name("header_area")
this is error msg
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-ed6cc8fa6e76>", line 14, in <module>
driver.find_elements_by_class_name("header_area")
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 580, in find_elements_by_class_name
return self.find_elements(by=By.CLASS_NAME, value=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: Unsupported locator strategy: null
(Session info: chrome=80.0.3987.132)
This is a newly introduced Chromedriver bug.
Try using an older version of Chrome or another browser. I just tried your code in Firefox 73 and it worked.
Hope this helps and good luck!
the bug seems to still be in chromedriver.
it's related to switching to a new window or tab.
However, in the OP's post, I don't see how he created the 2nd tab. he just switches to the tab/window with this
driver.switch_to.window(driver.window_handles[1])
It's possible the original problem here, was not creating the 2nd tab/window correctly.
Or it could be the existing chromedriver problem (which is intermittent/dependent on web page)
see here https://bugs.chromium.org/p/chromedriver/issues/detail?id=3390
The exception I'm tracking (which the OP reported) is this
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: Unsupported locator strategy: null

Why is my Selenium xpath expression returning an [object attribute] instead of an element?

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.

Using css selector in webdriver, gives an error other element would receive the click?

So the following code, I am using the first commented out option:
driver.find_element_by_css_selector("div.show_hide:nth-child(7) > h5:ntchild(1)").click()
Other options I have tried:
driver.find_element_by_xpath('// *[ # id = "show-hide"] / h5').click()
driver.find_element_by_css_selector('#show-hide > h5').click()
Sorry about the formatting, the site keeps giving error even when its properly formatted as code.
The url
I have a list of stocks and sometimes the code works perfectly but other times like now I get the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 71, in opt_data
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <h5>...</h5> is not clickable at point (791, 654). Other element would receive the click: <h5>...</h5>
(Session info: chrome=60.0.3112.90)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.15063 x86_64)
I have tried using other selectors but nothing seems to click the element correctly except the one I have in the code currently.
Wondering what I am doing wrong or what would be a better solution.
It would be very difficult to tell you exactly what is wrong, cause it is a valid webdriver behavior. Basically, web driver tells you that another element is visually on top of the one, that you are trying to click. It may be caused by several things:
1) Bad test design (i.e. something is really on top of your element. - check your test scenario and/or element layout
2) Difference in screen resolution/browser size when running the test. - if you are not running headless, then you could visually check what is happening. alternatively, set the browser size to a predefined size before each test start.
3) Very unlikely, malfunction of the web driver...
There one possible work around for such issues, if you are sure that everything is done correctly - fall back to the good old browser.execute_script. You can pass the problematic states of your test scenario by running those in injected javascript inside the browser itself. However, it will not do the validation, that webdriver does. So use it with care.

selenium webdriver locate the element

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();

CLOSED - Python Selenium Can't find elements

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");

Categories