【问题标题】:Click a label checkbox in Python Selenium for Accept Terms and Conditions单击 Python Selenium 中的标签复选框以接受条款和条件
【发布时间】:2021-12-12 02:07:06
【问题描述】:

您好,我想点击此复选框:

<label class="sui-AtomCheckbox sui-AtomCheckbox--medium">
    <input type="checkbox" id="isConsentApproved" name="isConsentApproved" intermediate="" value="false" tabindex="5">
</label>

但我无法点击,因为 Python 会引发此错误:

no such element: Unable to locate element: {"method":"xpath","selector":"//label[@id="isConsentApproved"]"}

我的目标是标记checkbox 以接受条款和条件。

这是我现在的代码:

    driver.maximize_window()
    
    driver.get("https://www.milanuncios.com/registro")
    driver.implicitly_wait(5)
    button = driver.find_element_by_class_name(u"sui-AtomButton--solid")
    ActionChains(driver).move_to_element(button).click(button).perform()
    driver.find_element_by_id("name").send_keys(name)
    driver.implicitly_wait(5)
    driver.find_element_by_id("email").send_keys(email)
    driver.find_element_by_id("password").send_keys(password)
    driver.find_element_by_xpath('//label[@id="isConsentApproved"]').click()

--------------(编辑)------

感谢Frenchy的合作,Python找到了标签,但现在说这个不可点击的元素: selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互

任何人都可以解决这个问题。新代码是这样的:

driver.maximize_window()

driver.get("https://www.milanuncios.com/registro")
driver.implicitly_wait(5)
button = driver.find_element_by_class_name(u"sui-AtomButton--solid")
ActionChains(driver).move_to_element(button).click(button).perform()
driver.find_element_by_id("name").send_keys(name)
driver.implicitly_wait(5)
driver.find_element_by_id("email").send_keys(email)
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_xpath(
    '//input[@id="isConsentApproved"]').click()

---------------(编辑)------ 现在我可以使用 JavaScript 来点击复选框,然后开始工作!但是有一个问题...

点击后,selenium 关闭且不显示任何错误

这是我的新代码:

driver.maximize_window()

driver.get("https://www.milanuncios.com/registro")
driver.implicitly_wait(5)
button = driver.find_element_by_class_name(u"sui-AtomButton--solid")
ActionChains(driver).move_to_element(button).click(button).perform()
driver.find_element_by_id("name").send_keys(name)
driver.implicitly_wait(5)
driver.find_element_by_id("email").send_keys(email)
driver.find_element_by_id("password").send_keys(password)
element=driver.find_element_by_xpath('//input[@id="isConsentApproved"]')
driver.execute_script("arguments[0].click();", element)

有什么解决办法吗?

【问题讨论】:

  • 没有理由只为一个 ID 使用 XPath,例如driver.find_element_by_xpath('//label[@id="isConsentApproved"]') 应该是 driver.find_element_by_id("isConsentApproved")。如果您这样做,您将避免出现错误,因为 ID 不在 LABEL 上,而是在 INPUT 上。
  • 这能回答你的问题吗? How to change HTML code with Selenium Python?
  • 这是您几天前提出的问题的重复。不要再问同样的问题...耐心等待原始问题的答案。
  • 现在 Python 找到了标签按钮,但他说不是可交互的元素。你现在可以看到我的代码了。是的,我知道使用 id 更容易,但我想了解 xpath,因为我刚刚开始,我想确切地知道如何使用它:)
  • 对那个 JeffC 感到抱歉,但我没有正确发布另一篇文章,因为它们是我的第一篇文章,我不太理解,现在我理解得更好了,我决定用所有内容创建另一个必要的信息。

标签: javascript python selenium-webdriver


【解决方案1】:

不是

driver.find_element_by_xpath(
    '//label[@id="isConsentApproved"]').click()

它:

driver.find_element_by_xpath(
    '//input[@id="isConsentApproved"]').click()

编辑

所以你不能点击输入按钮,因为它不可点击,唯一要做的就是点击标签:

(确保元素没有被初始窗口覆盖)

label = driver.find_element_by_xpath('//label[@class="ma-FormRegister-legal sui-AtomLabel sui-AtomLabel--small sui-AtomLabel--inlineLeft"]')
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(label).click(label).perform()

【讨论】:

  • 嗨,他快到了!但现在用这个 Python 说:selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  • 输入按钮不可点击,它是灰色的,所以你必须在与 Actionchains 关联的标签上手动执行点击,请参阅我的回答编辑
  • 嗨,我遇到了一个使用 javascript 的解决方案。 Python选中框,但在检查1秒后崩溃时。我要编辑帖子
  • 你发送的最后一个解决方案对我不起作用,完成发送密码后崩溃。
猜你喜欢
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多