javascriptpythonselenium

Python selenium selecting an invisible checkbox


I am trying to uncheck a checkbox using selenium in Python. However, I got the following error message:

selenium.common.exceptions.ElementNotVisibleException:
Message: Element is not currently visible and so may not be interacted with

I am wondering how should I make it visible?

An interesting part of this checkbox is that it contains some JavaScript, and I am not sure if this is the place caused the trouble. I tried the following methods but got the same error.

driver.find_element_by_id("1986 Thru 1990").click()

or

driver.find_element_by_xpath('//*[@id="1986 Thru 1990"]').click()

enter image description here


Solution

  • Try to add some time to wait until element become visible:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.visibility_of_element_located((By.ID,'1986 Thru 1990')))
    element.click()
    

    Let me know if issue still persist