Click a checkbox with selenium-webdriver
Asked Answered
B

4

5

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)

enter image description here

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")
Bakst answered 7/9, 2015 at 21:17 Comment(5)
Can you provide the exact url? I cannot see the checkox on log in page? Plus, provide the exact css/xpath you tried to see if there is any bug in themLaunch
tumblr.com/login is the exact url, they checkbox will appear when when you have logged in and out a couple times via tumblr. @LaunchBakst
Cannot reproduce. tried at least 5 timesLaunch
The attributes mention recaptcha... is this some bot prevention dialog?Krueger
Its the captcha checkbox thingy, I'm trying to click it @KruegerBakst
A
-3

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()
Agrigento answered 8/9, 2015 at 8:42 Comment(0)
M
7

Seems you can’t use a selector directly. Instead select an element before the reCaptcha, then use send_keys to tab to the checkbox, and space to tick it. See final line of code below for example.

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

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 + " ")
Markova answered 27/5, 2017 at 3:26 Comment(0)
L
0

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');")
Launch answered 7/9, 2015 at 22:14 Comment(7)
when I use this code, it'll just click it or something?Bakst
It should check the checkbox. And, I assumed it would work without knowing the dom structureLaunch
i tried a wait 10 seconds and then your code, but still got an errorBakst
I really need to see what's happening on the page. It's hard to pinpoint the issue without knowing the actual causeLaunch
can we go into a chat?Bakst
Not directly from hereLaunch
Let us continue this discussion in chat.Bakst
E
0

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);
Examinee answered 7/10, 2018 at 15:4 Comment(0)
A
-3

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()
Agrigento answered 8/9, 2015 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.