python - Selenium Webdriver (Python) - 单击 div 元素(复选框)

标签 python html selenium selenium-webdriver iframe

我正在尝试单击 iframe 中的非按钮元素(复选框),但无法使用 selenium 网络驱动程序单击它。 URL 是 https://www.nissanoflithiasprings.com/schedule-service,我要单击的框如下面的屏幕截图所示:

注意:选择以下选项可到达以下屏幕截图所示的屏幕:单击新客户“MAKE.MODEL.YEAR”。选择 Make 为“NISSAN” -> Year 为“2018” -> Model 为“ALTIMA” -> Trim 为“SL” -> Engine Type 为“I4” -> Enter Mileage 为“35000” -> 点击“CONTINUE”在底部。在下一页上,目标是单击复选框 Maintenance Package Standard/Schedule 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()

截图: enter image description here

您可能想使用 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() 

关于python - Selenium Webdriver (Python) - 单击 div 元素(复选框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456186/

相关文章:

python - Tkinter 变量类的第一个参数用于什么?

javascript - :active时移动元素

javascript - 多个内容可编辑,无法使用箭头键将插入符号移动到跨度的末尾

google-chrome - 从 Eclipse IDE 调用 Selenium 脚本时出现 "Chrome Not Reachable"

python - 通过 python selenium 在 Chrome 中限制 CPU

python - 当它是 '' 时删除元组列表的元素

python - 如何在 sklearn 中将线性回归与分类变量结合使用

javascript - 为什么div添加后不能拖动?

java - 使用 Selenium 和 Java 进行自动化,如何制作脚本的面向对象部分 "actionable?"

python - 从旧数据框创建子列