Reputation: 41
I can't locate the check boxes on this site in Selenium
I have tried: Xpath, ID, Type, Actionkeys, Text and partial text. I've been able to send keys (user and password) and locate the user / pass elements.
Code:
label for="acceptTermsAndConditions" class="checkbox-label-margin"> -#I accept the Terms and Conditions
Thanks for your help in advance, I feel like I'm going around in circles.
Upvotes: 1
Views: 236
Reputation: 473893
TL;DR see code with clarifying comments below.
One of the reasons why it was not working for you could be that you needed to wait for the form to get rendered, visible and interactable. This could be solved with an Explicit Wait.
Another problem that I've noticed with just clicking the "Accept Terms" via .click()
was that, since the click happens in the middle of the element, it opens up Terms of Use in a separate tab which is undesirable. You could work this around by clicking with an offset of (0, 0)
with Action Chains.
As far as getting to the checkboxes with selenium locators, it could be done in a variety of different ways. In the code below, I am using CSS selectors while checking the value of for
attributes of label
elements.
The working code:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://onecall.1100.com.au/au-b4-en/Account/Login')
wait = WebDriverWait(driver, 10)
# wait for the form to get visible
login_form = wait.until(EC.visibility_of_element_located((By.ID, 'loginForm')))
# accept terms
accept_terms = login_form.find_element_by_css_selector('label[for=acceptTermsAndConditions]')
ActionChains(driver).move_to_element_with_offset(accept_terms, 0, 0).click().perform()
# keep me logged in
login_form.find_element_by_css_selector('label[for=checkbox2]').click()
# take a screenshot to prove it is working
login_form.screenshot('form.png')
And, this is what you are gonna see in form.png
:
Upvotes: 1