Click a checkbox with selenium-webdriver

こ雲淡風輕ζ 提交于 2020-01-14 14:31:52

问题


I'm testing my app with tumblr and I have to log in and out as I go through procedures. While doing so, I'm having trouble clicking a checkbox that keeps popping up. How can I use selenium-webriver in python to click it?

I've tried selecting xpaths, ...by_ids, and by_classes, they won't work, so now I'm trying to use the mouse's coordinates to physically click the item. (This is on the tumblr login page, fyi)

Above is the html of the item I'm trying to select.

(EDIT:) I've the following selectors:

#checkbox = driver.find_element_by_id("recaptcha-anchor")
#checkbox = driver.find_element_by_id("g-recaptcha") 
#driver.find_element_by_xpath("//*[@id='recaptcha-token']")
#driver.find_element_by_css_selector("#recaptcha-anchor")
#driver.find_element_by_xpath("//*[@id='recaptcha-anchor']")
#driver.find_element_by_id("recaptcha-token").click()
#driver.find_element_by_class_name('rc-anchor-center-container')
#checkbox = driver.find_element_by_id("recaptcha-anchor")

回答1:


Here is a simple example that works for me in Java:

driver.findElement(By.id("checkbox_id")).click();

In Python, it seems to be:

driver.find_element_by_id("checkbox_id").click()



回答2:


I realise this is an old thread, but I couldn't find the answer anywhere else. In the end I figured it out as follows.

Note 1: this will tick the recaptcha box, but it won't solve it, you'll still need to do that manually.

Note 2: this is on macOS, so you might need a different format for chrome_path on Windows

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#modify line below to location of your chromedriver executable
chrome_path = r"/Users/Username/chromedriver"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.btcmarkets.net/login")

username = driver.find_element_by_id("userIdText")
username.send_keys("Us3rn4me")

password = driver.find_element_by_id("userPasswordText")
password.send_keys("Pa55w0rD")

#the line below tabs to the recaptcha tickbox and ticks it with the space bar
password.send_keys(Keys.TAB + Keys.TAB + " ")



回答3:


Seems like this is not an input tag. So, probably manipulating the aria-checked attribute and set it to true would do it. The only way to change attribute value is JavaScriptExecutor. Try the following:

driver.execute_script("$('#recaptcha-anchor').setAttribute('aria-checked','true');")



回答4:


Use code below can find the checkbox with id "recaptcha-anchor" and click it, but unable to bypass it. The following pictures will pop up.

List<WebElement> frames = driver.findElements(By.tagName("iframe"));
    String winHanaleBefore = driver.getWindowHandle();
    driver.switchTo().frame(0);
driver.findElement(By.id("recaptcha-anchor")).click();
driver.switchTo().window(winHanaleBefore);


来源:https://stackoverflow.com/questions/32446151/click-a-checkbox-with-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!