javascript - 如何通过 Selenium Python 根据 HTML 在复选框内单击
问题描述
大家好,我试图单击此复选框:
<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()
部分是因为复选框似乎有鼠标,但它没有点击,你能帮帮我吗?
解决方案
将您的操作链接在一起可能有助于解决此问题。在调用方法之前结合move_to_element
操作。click
perform()
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()
推荐阅读
- mql4 - 请告诉我 MQL4 元编辑器中的交易功能 OrderOpenTime
- android - Room Database:如何一次性获取数据?
- azure-data-factory-2 - 数据工厂表达式子字符串?有没有类似的功能?
- bash - 使用 Git Bash 在某处复制文件夹的脚本
- video - 使用 FFMPEG 创建的视频无法在 IOS/Safari 上播放
- azure-sql-database - sys.sp_cdc_stop_job 不在服务器上
- php - ( wordpress ) elementor 投资组合类别(全部)
- php - 如何使用 PHP 和 imagemagick 改变图像的形状?
- reactjs - React JS TypeError:无法读取未定义的属性(读取“长度”)
- python - 在 Python 中查找二维数组(非常大)中的值