Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  12] [ 1]  / answers: 1 / hits: 34667  / 9 Years ago, tue, july 14, 2015, 12:00:00

I am using Selenium to save a webpage. The content of webpage will change once certain checkbox(s) are clicked. What I want is to click a checkbox then save the page content. (The checkboxes are controlled by JavaScript.)



Firstly I used:



driver.find_element_by_name(keywords_here).click()


it ends with an error:



NoSuchElementException


then I tried “xpath” like, with implicit/explicit waiting:



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 an error:



ElementNotVisibleException


Posts



How to force Selenium WebDriver to click on element which is not currently visible?



Selenium Element not visible exception



suggest it should make the checkboxes visible before clicking, for example using:



execute_script


The question may sounds stupid, but how can I find out the proper sentence to “execute_script” the visibility of checkbox from the page source code?



Besides that, is there another way?



Thanks.



by the way, the line html code looks like:



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


its xpath looks like:



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

More From » python

 Answers
80

Alternative option would be to make the 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 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()

[#65819] Saturday, July 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nadineannabellet

Total Points: 464
Total Questions: 94
Total Answers: 97

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
;