Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium generating error "Element is not interactable"

enter image description here

I am trying to use Selenium to click the button highlighted above. I have no problem locating the element via:

download_button_path = "//button[@class='btn-primary']"
download_button = driver.find_element_by_xpath(download_button_path)

However when I try and execute:

download_button.click()

I get the error message:

ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=70.0.3538.67)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.11.6 x86_64)

It seems the button is not visible to selenium even though I am able to see it when performing the click manually.

I have also tried to hover over the button and then click, as well as send an Enter/Return key to the button, but nothing is working.

Any insight would be appreciated.

like image 919
Daniel Schissler Avatar asked Oct 17 '18 16:10

Daniel Schissler


3 Answers

In the HTML, I see the btn-primary is present in a bootstrap modal popup. So there may another btn-primary behind the modal pop. The XPath will be finding the element which is behind the modal which is not interactable.

btn-primary class is a generic class in bootstrap that will be used in all primary buttons. Try with unique locator with reference to modal element as a parent in your locator

download_button_path = "//[@class='lmn-edititem-modal']/../[@class=''btn-primary']"
wait = WebDriverWait(driver, 10)
download_button = wait.until(EC.visibility_of_element_located((By.XPATH, download_button_path)))
download_button .click()

We can also try this with CSS selector

driver.find_elements_by_css_selector(".lmn-edititem-modal .btn-primary") 
like image 143
Navarasu Avatar answered Nov 13 '22 11:11

Navarasu


For me, extending relative Xpath just with its parent helped.

button = driver.find_element_by_xpath("//button[@data-value='0']")
button.click()
#this did not work

button = driver.find_element_by_xpath("//section[2]/button[@data-value='0']")
button.click()
#this worked well
like image 5
Thomasko Avatar answered Nov 13 '22 11:11

Thomasko


Have you tried hovering over the button and then clicking?

try the following:

button_to_click = driver.find_element_by_xpath('button_to_click's xpath')
hover = ActionChains(driver).move_to_element(button_to_click)
hover.perform()
button_to_click.click()

Hope this helps.

like image 2
Lucca Frare Avatar answered Nov 13 '22 11:11

Lucca Frare