13

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.

Ger Cas
  • 2,188
  • 2
  • 18
  • 45
  • 1
    Are you *scraping data* or *running tests for the usability of a website you are coding*? The solution will be drastically different for these cases – Reedinationer May 17 '19 at 22:17
  • @Reedinationer Hi, I only want to automate several steps that I need to do very often on a website and takes some time. Is not a website that I'm coding. – Ger Cas May 17 '19 at 22:31

4 Answers4

60

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.

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • 4
    Excellent. Thanks so much for your help, it works perfect!. Where can I see examples of selenium using Javascript to fix issues like this?. Until now I thougth I was working Selenium only with Python hehe, but I see that can execute more poweful options adding Javascript. Then `arguments` is an array? since you used `arguments[0]`. – Ger Cas May 17 '19 at 23:00
  • 1
    @GerCas I just took that example from [this guy](https://stackoverflow.com/a/37880313/10924296) and don't really know how to code javascript too well. From [the documentation](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script) it looks like `arguments` will just unpack anything you feed it. So maybe `driver.execute_script("arguments[0].click();arguments[1].click()", element1, element2)` would be valid, but I've not needed anything that complicated. Best of luck figuring it out! – Reedinationer May 17 '19 at 23:07
  • Thank you for the help. – Ger Cas May 17 '19 at 23:15
  • 1
    You save my project, my job and my life ! Thanks dude for the help – Adrien Forbu Dec 02 '20 at 14:32
3

Sometimes you may need to copy Full XPATH. That was one work around I found.

aiishwin09
  • 31
  • 1
2

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.

Rola
  • 1,598
  • 13
  • 12
1

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

1000 Years
  • 33
  • 6