Python and Selenium For "execute_script" for "ElementNotVisibleException"

I am using Selenium to save a webpage. The content of the web page will change after clicking certain checkboxes. I want to click a checkbox and then save the page content. (Checkboxes are driven by JavaScript.)

First, I used:

driver.find_element_by_name("keywords_here").click()

      

it ends up with an error:

NoSuchElementException

      

then I tried "xpath" like, with implicit / explicit expectation:

URL = "the url"

verificationErrors = []
accept_next_alert = True

aaa = driver.get(URL)
driver.maximize_window()
WebDriverWait(driver, 10)

#driver.find_element_by_xpath(".//*[contains(text(), ' keywords_here')]").click()
#Or: 

driver.find_element_by_xpath("//label[contains(text(),' keywords_here')]/../input[@type='checkbox']").click()

      

it gives error:

ElementNotVisibleException

      

Posts

How do I get Selenium WebDriver to click on an element that is not currently visible?

Selenium Element not visible

suppose checkboxes need to be done before clicking, for example using:

execute_script

      

The question may sound silly, but how can I find the correct sentence for the "execute_script" checkbox visibility from the page source code?

Also, is there any other way?

Thank.

By the way, the html string code looks like this:

<input type="checkbox" onclick="ComponentArt_HandleCheck(this,'p3',11);" name="keywords_here">

      

its xpath looks like this:

//*[@id="TreeView1_item_11"]/tbody/tr/td[3]/input

      

+4


source to share


2 answers


An alternative would be to do click()

inside execute_script()

:

# wait for element to become present
wait = WebDriverWait(driver, 10)
checkbox = wait.until(EC.presence_of_element_located((By.NAME, "keywords_here")))

driver.execute_script("arguments[0].click();", checkbox)

      

where is EC

imported as:

from selenium.webdriver.support import expected_conditions as EC

      




Alternatively and as another snapshot in the dark, you can use the element_to_be_clickable

Expected Condition and click in the usual way:

wait = WebDriverWait(driver, 10)
checkbox = wait.until(EC.element_to_be_clickable((By.NAME, "keywords_here")))

checkbox.click()

      

+6


source


I had problems with expected conditions, I prefer to build my timeout.

import time
from selenium import webdriver
from selenium.common.exceptions import \
    NoSuchElementException, \
    WebDriverException
from selenium.webdriver.common.by import By

b = webdriver.Firefox()
url = 'the url'
b.get(url)
locator_type = By.XPATH
locator = "//label[contains(text(),' keywords_here')]/../input[@type='checkbox']"
timeout = 10
success = False
wait_until = time.time() + timeout
while wait_until < time.time():
    try:
        element = b.find_element(locator_type, locator)
        assert element.is_displayed()
        assert element.is_enabled()
        element.click()
        success = True
        break
    except (NoSuchElementException, AssertionError, WebDriverException):
        pass
if not success:
    error_message = 'Failed to click the thing!'
    print(error_message)

      



may need to add InvalidElementStateException and StaleElementReferenceException

+1


source







All Articles