【问题标题】:clicking checkbox using python selenium使用python selenium单击复选框
【发布时间】:2017-06-27 12:16:16
【问题描述】:

我有一个从 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()

并得到错误

Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    driver.find_element_by_xpath("//*[@id='specialty-106']").click()
  File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 70, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 404, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 195, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.46.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 170, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: unknown error: Element <input type="checkbox" id="specialty-106" name="Specialty[]" value="106"> is not clickable at point (281, 554). Other element would receive the click: <label for="specialty-106"></label>
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.1.7600 x86_64)

【问题讨论】:

  • 你的 xpath 字符串中是否真的有换行符?当我在该页面上尝试您的 xpath 时,只要 dl- 和 profession 之间没有空格,它似乎就可以工作。
  • 您应该在帖子中加入一个问题,而不是感谢。我删除了感谢,但不确定您是否想要提问“如果我改用 perl,selenium 会遇到同样的问题吗?”或者如果你想问别的。所以我让你想出一个适当的疑问句,编辑你的帖子并添加它。

标签: python selenium checkbox


【解决方案1】:

这是您问题的答案:

此代码块将打开网址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()

如果这能回答您的问题,请告诉我。

【讨论】:

    【解决方案2】:

    回溯包含对问题的非常清晰的解释。

    WebDriverException:消息:未知错误:元素是 在点 (281, 554) 处不可点击。其他元素将收到 点击:

    您可能必须在两次点击之间添加延迟,以等待 DOM 更新,以便元素可点击。

    【讨论】:

    • 实际上 dl- 和专业之间没有空格,但在我的最后仍然不起作用。我使用了 3 秒的隐式等待,但它不起作用。
    • 我确实尝试通过执行 myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'specialty-106'))) 等待,但仍然无法点击。
    猜你喜欢
    • 2019-10-10
    • 2022-01-07
    • 1970-01-01
    • 2017-09-24
    • 2021-12-11
    • 2021-12-23
    • 2020-10-06
    • 2018-10-31
    • 1970-01-01
    相关资源
    最近更新 更多