javascript - 如何通过 Selenium Python 根据 HTML 单击复选框

标签 javascript python selenium xpath css-selectors

大家好,我正在尝试单击此复选框:

<label class="has-checkbox terms"><input name="order[terms]" type="hidden" value="0" /><input class="checkbox" type="checkbox" value="1" name="order[terms]" id="order_terms" />I have read and agree to the <a href="http://www.supremenewyork.com/shop/terms">terms & conditions</a>, and accept the return policy<span class="terms-error">please agree to the terms</span></label></p><div class="g-recaptcha" data-callback="checkoutAfterCaptcha" data-sitekey="AAAA3423" data-size="invisible"></div><input id="number_v" name="hpcvv" /></fieldset></div></div><div id="cart-footer"><div id="pay"><p style="">Surgelati</p><input type="submit" name="commit" value="process payment" class="button checkout" disable_with="processing, please wait..." /><a class="button cancel" href="http://www.altervista.com/shop">cancel</a></div></div></form></div><div id="surchage_info_tooltip">Vendita 

我尝试过:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("order_terms")

actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
element = driver.find_element_by_id('order_terms').click()

driver.find_element_by_class_name("has-checkbox terms").click()
driver.find_element_by_xpath(".//*[contains(text(), 'I have read and agree to the')]").click()

这些代码中的每一个,但没有一个有效......

这有效

actions.move_to_element(element).perform()

部分是因为该复选框上似乎有鼠标,但它没有点击,你能帮助我吗?

最佳答案

将您的操作链接在一起可能有助于解决此问题。在调用 perform() 方法之前,将 move_to_element 操作与 click 结合起来。

from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("order_terms")
actions = ActionChains(driver)
action.move_to_element(element).click(element).perform()

或者简单地说

action.move_to_element(element).click().perform()

关于javascript - 如何通过 Selenium Python 根据 HTML 单击复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321116/

相关文章:

Python selenium - 修改网页的源代码

javascript - 来自 C# 的 Highcharts 范围选择器日期格式

python - 反向在 get_absolute_url 中返回一个空字符串

javascript - 从 JavaScript 调用 Blazor 方法

python - 从 URL 获取时如何在 python 中获取图像文件大小(在决定保存之前)

python - 如何在 Python 上解析 "ImportError: DLL load failed:"?

ruby - selenium 2.4.0,如何检查是否存在警报

java - Selenium WebDriver : How to make sure element availability on Web Page?

javascript - 在弹出窗口中隐藏网址

Javascript 正则表达式 : only match the end of text