简体   繁体   English

如何通过 Selenium Python 根据 HTML 在复选框内单击

[英]How to click within the checkbox as per the HTML through Selenium Python

Hi guys i was trying to click this checkbox :嗨,伙计们,我试图单击此复选框:

 <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

I have tried with :我试过:

 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()

Every of this codes but no-one of them works....这些代码中的每一个,但没有一个有效......

This works这有效

actions.move_to_element(element).perform()

partially because the checkbox appear to have the mouse on it , but it doesn't click , can you help me ?部分是因为复选框上似乎有鼠标,但它没有点击,你能帮帮我吗?

Chaining your actions together might help solve this problem.将您的操作链接在一起可能有助于解决此问题。 Combine move_to_element action with click before calling the perform() method.在调用perform()方法之前,将move_to_element操作与click结合move_to_element

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()

OR simply或者干脆

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

As per the HTML you have shared to invoke click() on the checkbox you can use either of the following solutions:根据您共享以在复选框上调用click()HTML ,您可以使用以下任一解决方案:

  • CSS_SELECTOR : CSS_SELECTOR :

     driver.find_element_by_css_selector("label.has-checkbox.terms input.checkbox#order_terms").click()
  • XPATH : XPATH

     driver.find_element_by_xpath("//label[@class='has-checkbox terms']//input[@class='checkbox' and @id='order_terms']").click()

Update更新

As you are seeing the error Other element would receive the click you can adopt either of the following solutions:当您看到其他元素会收到点击错误时您可以采用以下任一解决方案:

  • Induce WebDriverWait :诱导WebDriverWait

    • CSS_SELECTOR : CSS_SELECTOR :

       WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.has-checkbox.terms input.checkbox#order_terms"))).click()
    • XPATH : XPATH

       WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='has-checkbox terms']//input[@class='checkbox' and @id='order_terms']"))).click()
  • Using WebDriverWait and ActionChains :使用WebDriverWaitActionChains

    • CSS_SELECTOR : CSS_SELECTOR :

       myElement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.has-checkbox.terms input.checkbox#order_terms"))) ActionChains(driver).move_to_element(myElement).click(myElement).perform()
    • XPATH : XPATH

       myElement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='has-checkbox terms']//input[@class='checkbox' and @id='order_terms']"))) ActionChains(driver).move_to_element(myElement).click(myElement).perform()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM