TTT
TTT

Reputation: 4434

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

Upvotes: 1

Views: 815

Answers (2)

hoju
hoju

Reputation: 29452

Instead of waiting for the checkbox to be visible you could use JavaScript to change the state directly:

var checkbox = driver.find_element(By.XPATH, '//*[@id="1986 Thru 1990"]'):
driver.execute_script('var element = arguments[0]; element.checked=false', checkbox)

Upvotes: 0

Andersson
Andersson

Reputation: 52665

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

Upvotes: 1

Related Questions