Reputation: 110
I am trying to automate one part of my project I've been working on. This part of the project should enter a page and get a randomly generated key. To get the key it goes to the web-page, login and then presses some buttons to get the new key. I've been using Selenium with Chrome driver to do so.
driver = webdriver.Chrome()
The problem started when I had to check some checkboxes.
This is how the page looks like: https://i.sstatic.net/4GHaD.jpg
Source file looks like its JavaScript rendered.
What I've tried so far: Getting it by id:
checkbox = label.find_element_by_id("agreed")
checkbox.click()
Getting it by XPath:
checkbox = driver.find_element_by_xpath('//*[@id="agreed"]')
Both are giving me:
Message: element not visible
I've also tried waiting for it to be visible but it just waits and eventually gives me the following:
checkbox = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="agreed"]')))
checkbox.click()
Output:
Message:
Upvotes: 2
Views: 668
Reputation: 193088
To click on the checkbox associated with the element with text as I agree with the API Servive Agreement instead of invoking visibility_of_element_located()
you need to invoke element_to_be_clickable()
and you can use either of the following solutions:
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='agreed']"))).click()
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='agreed']"))).click()
Upvotes: 2
Reputation: 43
Always have in mind using driver.implicitly_wait(X)
where X is a number of seconds to make your driver to automatically retry when an element is not found. Besides browser automation have you tried capturing the requests? In similar cases I've managed to automate the post request using cookies from the session. If you are unlucky and there's session timeout after a while you can use your selenium script to renew your cookies and continue with automated requests.
Upvotes: 1
Reputation: 1275
You should check in console if that id is returning only one element or more then one. Sometimes more then one elements are present in DOM for same id or selector. In which one is hidden and one is visible. you have to find the visible one and click on it. Also check if that element is not present in an iframe.
In console write below lines to see the number of elements for this id :
$x("//*[@id='agreed']")
Hope that helps you.
Upvotes: 2