Python and Selenium To “execute_script” to solve “ElementNotVisibleException”
Alternative option would be to make the click()
inside execute_script()
:
# wait for element to become presentwait = WebDriverWait(driver, 10)checkbox = wait.until(EC.presence_of_element_located((By.NAME, "keywords_here")))driver.execute_script("arguments[0].click();", checkbox)
where EC
is imported as:
from selenium.webdriver.support import expected_conditions as EC
Alternatively and as an another shot in the dark, you can use the element_to_be_clickable
Expected Condition and perform the click in a usual way:
wait = WebDriverWait(driver, 10)checkbox = wait.until(EC.element_to_be_clickable((By.NAME, "keywords_here")))checkbox.click()
I had some issues with expected conditions, I prefer building my own timeout.
import timefrom selenium import webdriverfrom selenium.common.exceptions import \ NoSuchElementException, \ WebDriverExceptionfrom selenium.webdriver.common.by import Byb = webdriver.Firefox()url = 'the url'b.get(url)locator_type = By.XPATHlocator = "//label[contains(text(),' keywords_here')]/../input[@type='checkbox']"timeout = 10success = Falsewait_until = time.time() + timeoutwhile wait_until < time.time(): try: element = b.find_element(locator_type, locator) assert element.is_displayed() assert element.is_enabled() element.click() # or if you really want to use an execute script for it, you can do that here. success = True break except (NoSuchElementException, AssertionError, WebDriverException): passif not success: error_message = 'Failed to click the thing!' print(error_message)
might want to add InvalidElementStateException, and StaleElementReferenceException