简体   繁体   English

如何使用 Selenium Python 单击此复选框?

[英]How can I click on this check box using Selenium Python?

I can click all of the other checkboxes on the page.我可以单击页面上的所有其他复选框。 But when it comes to this one, it won't allow me to click on it但是说到这个,就不允许我点击

The HTML code for the checkbox is:复选框的 HTML 代码是:

<input id="ContentPlaceHolder1_wucSignInStep2_chkTC" type="checkbox" name="ctl00$ContentPlaceHolder1$wucSignInStep2$chkTC">

My code for clicking the text box:我点击文本框的代码:

element = driver.find_element_by_xpath('//span[span/input[@name="checkbox checkbox-primary"]]').click()

I can provide the full code if required.如果需要,我可以提供完整的代码。

There is an id associated with your input field!有一个与您的输入字段相关联的id You can use the id to find the element您可以使用 id 来查找元素

element = driver.find_element_by_id('ContentPlaceHolder1_wucSignInStep2_chkTC').click()

That should do it.那应该这样做。

If you are getting an element not visible error then you can try the following:如果您收到element not visible错误,则可以尝试以下操作:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("ContentPlaceHolder1_wucSignInStep2_chkTC")

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

The above code will make the element visible and also, put the mouse cursor over the checkbox.上面的代码将使元素可见,并将鼠标光标放在复选框上。

I tried to follow this instruction, and it works for me.我尝试按照此说明进行操作,它对我有用。

https://www.toolsqa.com/selenium-webdriver/selenium-checkbox/ https://www.toolsqa.com/selenium-webdriver/selenium-checkbox/

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

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