Search code examples
pythonpython-3.xselenium-webdriverxpathwebdriverwait

Python: Selenium cann't finde the btn on pop-up window


I'm trying to click the btn "select all cookies" on cookies terms pop-up window. I'v tried:

div=driver.find_element(By.XPATH,'//*[@id="js_cookie-settings-modal"]/div[2]/div/div[3]/div')
driver.implicitly_wait(4)
button=div.find_element(By.XPATH,'//*[@id="selectAll"]')
driver.implicitly_wait(4)
button.click()

Error:

  raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=111.0.5563.64)

Option2:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="selectAll"]'))).click()
driver.implicitly_wait(50)

Error:

   raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

URL:https://www.eversports.de/


Solution

  • if you use following xpath //*[@id="selectAll"]', it is going to identify two elements, where first one is not interactable.

    Use below xpath to uniquely identify the element. You don't need to use parent element, you can use this xpath to identify and click on the button.

    button=driver.find_element(By.XPATH,'//button[@id="selectAll" and text()="Accept all"]')
    button.click()
    

    Ideally you should use WebDriverWait() to handle dynamic page element.

    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()
    

    you need following imports.

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

    Update:

    driver.get("https://www.eversports.de/")
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()
    

    It might possible cookie option will come for the first time and once accept it, it will never appear. Use try..except block to handle this.

    driver.get("https://www.eversports.de/")
    try:
         WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()
    except:
         print("No popup found!")
         pass