使用python selenium单击复选框

2024-06-16 10:38:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小任务,从https://www.carecredit.com/doctor-locator/抓取数据。 我无法使用我的脚本执行复选框勾选。在

我正在做

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
driver = webdriver.Chrome()
driver.get('https://www.carecredit.com/doctor-locator/')
driver.find_element_by_xpath("//select[@id='dl-
             profession']/option[@value='9']").click()
driver.find_element_by_xpath("//*[@id='specialty-106']").click()

把错误当作

^{pr2}$

Tags: fromhttpsimportcomsupportbywwwdriver
2条回答

以下是您问题的答案:

此代码块将打开URL https://www.carecredit.com/doctor-locator,单击Profession下拉列表,选择Weight Loss,最后选中复选框Weight Loss Surgery。在

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path= r"C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('https://www.carecredit.com/doctor-locator/')
select = Select(driver.find_element_by_id('dl-profession'))
select.select_by_value("9")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='specialty-106']//following::label[1]")))
driver.find_element_by_xpath("//input[@id='specialty-106']//following::label[1]").click()

如果这回答了你的问题,请告诉我。在

回溯包含了一个非常清楚的错误解释。在

WebDriverException: Message: unknown error: Element is not clickable at point (281, 554). Other element would receive the click:

您可能需要在两次单击之间添加一个延迟,以等待DOM更新,以便元素可以单击。在

相关问题 更多 >