[英]Selenium Webdriver (Python) - Click on a div element (checkbox)
我正在嘗試單擊 iframe 內的非按鈕元素(復選框),但無法使用 selenium webdriver 單擊它。 URL 是https://www.nissanoflithiasprings.com/schedule-service ,我想點擊的框顯示在下面的屏幕截圖中:
注意:選擇以下選項以進入以下屏幕截圖所示的屏幕: 單擊新客戶“MAKE. MODEL. YEAR”。 選擇制造為“NISSAN”-> 年份為“2018”-> 模型為“ALTIMA”-> 修剪為“SL”-> 發動機類型為“I4”-> 輸入里程為“35000”-> 點擊“繼續”在底部。 在下一頁上,目標是單擊復選框維護包標准/附表 1(需要單擊該復選框,但我找不到 Selenium 的正確代碼行才能單擊它)
下面是我編寫的工作 Selenium Python 腳本,它成功導航到顯示我希望單擊的復選框的頁面:
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
# ---??????????????????
有人可以用正確的代碼幫助我使 selenium webdriver 單擊該復選框嗎?
該元素被另一個元素覆蓋...
另外,我將expected_conditions
更改為element_to_be_clickable()
。
所以你可以使用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()
您可能想使用CSS_SELECTOR
而不是 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()
試試這個定位器(//*[@class="checkbox"])[1]
並使用ActionChains
。
chk = wait.until(ec.element_to_be_clickable((By.XPATH, '(//*[@class="checkbox"])[1]')))
ActionChains(driver).move_to_element(chk).click().perform()
如果需要,您可以將[1]
更改為[2]
或[3]
似乎是您的復選框未啟用。 請在 is_selected() 之后設置一些條件...並檢查它現在是否已啟用..... http://selenium-interview-questions.blogspot.com/2014/03/working-with-checkboxes-in-硒.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.