Home > Back-end >  SELENIUM(PYTHON) ERROR: element not interactable
SELENIUM(PYTHON) ERROR: element not interactable

Time:03-05

Does anyone know how to click on this particular web element from this site?(enter image description here

I have tried in many different ways but i still get the same error(element not interactable) my code looks like this:

wd.find_element(By.XPATH,"//*[@id='mainbox']/div[2]/div[2]/div[4]/div/div[1]/div/button[4]").click()

The element is actually detected but for some reason it isn't clickable. Thanks in advance

CodePudding user response:

I can think of two possible explanations:

  1. The element you detected is not the element you think was detected. You're using a rather long XPATH, so I would be suspicious without external confirmation that you really selected the button you wanted. If the button you did select is disabled, then it will not be interactable.
  2. The button might not be clickable through the same method that selenium's .click() expects. I am not familiar with ng- attributes, so maybe it's just a language localization thing, but I suspect that selenium may be looking for an onclick attribute.

My suggestions:

  1. Instead of using a complicated XPATH, consider selecting by the inner text of the button. So something like this: //*button[text()=Successiva]
  2. Instead of using any xpath at all, just call the JavaScript function that is referenced in the button: getDataTableNextClick()

CodePudding user response:

wait=WebDriverWait(driver,20)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
except:
    pass

To simply click on that element just look for the button with that text.

To go through 15 pages you could do

current=1
last=15
while True:
    try:
        if current == last:
            break
        current =1
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
    except:
        break

Now this site is a little trickier if you want to go pass the 15 pages since the pagination doesn't disable the button.

current="1"
old="2"
while True:
    try:
        if current == old:
            break
        old=current
        wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='page-item']/button[.='Successiva']"))).click()
        current=wait.until(EC.visibility_of_element_located((By.XPATH,"//*[contains(@class,'page-item ng-scope active')]"))).text
    except:
        break

Import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
  • Related