2

I am trying to click on a non-button element (checkbox) inside an iframe but am not able to click on it using the selenium webdriver. The URL is https://www.nissanoflithiasprings.com/schedule-service and the box that I want to click is shown in the screenshot below:

NOTE: Select the following options to reach the screen shown in the screenshot below: Click on New Customer "MAKE. MODEL. YEAR". Select Make as "NISSAN" -> Year as "2018" -> Model as "ALTIMA" -> Trim as "SL" -> Engine Type as "I4" -> Enter Mileage as "35000" -> Click on "CONTINUE" at the bottom. On the following page, the goal is to click on the checkbox Maintenance Package Standard / Schedule 1 (the checkbox needs to be clicked but I am not able to find the correct code line for Selenium to be able to click on it)

Below is the working Selenium Python script that I wrote to successfully navigate until the page that shows the checkbox that I wish to click:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

chrome_path = r"C:\Users\gh456\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.maximize_window()
driver.get("https://www.nissanoflithiasprings.com/schedule-service")

wait = WebDriverWait(driver, 10)

# first frame - by css selector
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '[src^="https://consumer.xtime.com"]')))

# second frame - by ID
wait.until(ec.frame_to_be_available_and_switch_to_it('xt01'))

driver.find_element_by_id("new_customer_button").click()

driver.find_element_by_id("NISSAN").click()

wait.until(ec.visibility_of_element_located((By.ID, "2018"))).click()

wait.until(ec.visibility_of_element_located((By.ID, "ALTIMA"))).click()

wait.until(ec.visibility_of_element_located((By.ID, "SL"))).click()

wait.until(ec.visibility_of_element_located((By.ID, "I4"))).click()

wait.until(ec.visibility_of_element_located((By.NAME, "mileage_input"))).send_keys("35000")

wait.until(ec.visibility_of_element_located((By.ID, "continue_button"))).click()

# Click on the checkbox 
# ---??????????????????

Can someone help me with the correct code to make selenium webdriver to click that checkbox?

3 Answers 3

2

The element is covered by another element...

Also, I changed the expected_conditions as element_to_be_clickable().

So you can use ActionChains:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains


chrome_path = r"C:\Users\gh456\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.maximize_window()
driver.get("https://www.nissanoflithiasprings.com/schedule-service")

wait = WebDriverWait(driver, 10)

# first frame - by css selector
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '[src^="https://consumer.xtime.com"]')))

# second frame - by ID
wait.until(ec.frame_to_be_available_and_switch_to_it('xt01'))

driver.find_element_by_id("new_customer_button").click()

driver.find_element_by_id("NISSAN").click()

wait.until(ec.element_to_be_clickable((By.ID, "2018"))).click()

wait.until(ec.element_to_be_clickable((By.ID, "ALTIMA"))).click()

wait.until(ec.element_to_be_clickable((By.ID, "SL"))).click()

wait.until(ec.element_to_be_clickable((By.ID, "I4"))).click()

wait.until(ec.visibility_of_element_located((By.NAME, "mileage_input"))).send_keys("35000")

wait.until(ec.element_to_be_clickable((By.ID, "continue_button"))).click()

check_box_el = wait.until(ec.visibility_of_element_located((By.XPATH, '//div[@id="maintenance_package_section"]//label[@class="checkbox"]')))
ActionChains(driver).move_to_element(check_box_el).click().perform()

Screenshot: enter image description here

You might want to use CSS_SELECTOR and not XPATH:

check_box_el = wait.until(ec.visibility_of_element_located((By.CSS_SELECTOR, '#maintenance_package_section > div label')))
ActionChains(driver).move_to_element(check_box_el).click().perform() 
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Moshe! Your code gives me an error. Please click on ibb.co/f9bvc03 to see the screenshot of the error. Please help me how can I fix this. Thanks!
@SiddharthGosalia I don't see this error by me... what line of code generates this error?
The last line of the code beginning with ActionChains is throwing the error. See more details here: ibb.co/34cgS6k.
@SiddharthGosalia did you import it? from selenium.webdriver.common.action_chains import ActionChains
I figured out the reason for the error. The button to be clicked was not visible within the browser window on my laptop. So I just had to add an extra line of code along with your code which would scroll to the top of the page before clicking on the element: check_box_el = wait.until(ec.visibility_of_element_located((By.CSS_SELECTOR, '#maintenance_package_section > div label'))) driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME) time.sleep(1) ActionChains(driver).move_to_element(check_box_el).click().perform()
|
1

Try this locator (//*[@class="checkbox"])[1] and use ActionChains.

chk = wait.until(ec.element_to_be_clickable((By.XPATH, '(//*[@class="checkbox"])[1]')))
ActionChains(driver).move_to_element(chk).click().perform()

You can change [1] to [2] or [3] if you want

1 Comment

Hi Frian! Your code gives me the same error as Moshe's code. Please click on ibb.co/f9bvc03 to see the screenshot of the error. Please help me how can I fix this. Thanks!
0

It seems to be your check box is not enabled. Please, have some conditions following is_selected()...And check whether it is enabled now..... http://selenium-interview-questions.blogspot.com/2014/03/working-with-checkboxes-in-selenium.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.