3

I want to select a checkbox using selenium in python. Following is the HTML of the checkbox. The span element is getting highlighted when hovering the mouse over checkbox

HTML

<div id="rc-anchor-container" class="rc-anchor rc-anchor-normal rc-anchor-light"> <div id="recaptcha-accessible-status" class="rc-anchor-aria-status" aria-hidden="true">Recaptcha requires verification. </div> <div class="rc-anchor-error-msg-container" style="display:none"><span class="rc-anchor-error-msg" aria-hidden="true"></span></div> <div class="rc-anchor-content"> <div class="rc-inline-block"> <div class="rc-anchor-center-container"> <div class="rc-anchor-center-item rc-anchor-checkbox-holder"><span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox" role="checkbox" aria-checked="false" id="recaptcha-anchor" tabindex="0" dir="ltr" aria-labelledby="recaptcha-anchor-label"><div class="recaptcha-checkbox-border" role="presentation"></div><div class="recaptcha-checkbox-borderAnimation" role="presentation"></div><div class="recaptcha-checkbox-spinner" role="presentation"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation"></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span></div> </div> </div> <div class="rc-inline-block"> <div class="rc-anchor-center-container"> <label class="rc-anchor-center-item rc-anchor-checkbox-label" aria-hidden="true" role="presentation" id="recaptcha-anchor-label"><span aria-live="polite" aria-labelledby="recaptcha-accessible-status"></span>I'm not a robot</label> </div> </div> </div> <div class="rc-anchor-normal-footer"> <div class="rc-anchor-logo-portrait" aria-hidden="true" role="presentation"> <div class="rc-anchor-logo-img rc-anchor-logo-img-portrait"></div> <div class="rc-anchor-logo-text">reCAPTCHA</div> </div> <div class="rc-anchor-pt"><a href="https://www.google.com/intl/en/policies/privacy/" target="_blank">Privacy</a><span aria-hidden="true" role="presentation"> - </span><a href="https://www.google.com/intl/en/policies/terms/" target="_blank">Terms</a></div> </div> </div> 

I am trying the following code but it is giving following exception selenium.common.exceptions.NoSuchElementException

My Code

from selenium import webdriver from selenium.webdriver.common.keys import Keys chromedriver = 'C:\Program Files (x86)\Google\Chrome\chromedriver' browser = webdriver.Chrome(chromedriver) browser.get(url) checkBox = browser.find_element_by_id("recaptcha-anchor") checkBox.click() 
9
  • @Pratik I added time.sleep(10) after browser.get(url) still not working.
    – harv3
    CommentedOct 17, 2019 at 20:46
  • Is it possible for you to share the url as I think you have to select div not the span
    – Pratik
    CommentedOct 17, 2019 at 20:52
  • Here is the URL portal.zinghr.com/2015/pages/authentication/login.aspx
    – harv3
    CommentedOct 17, 2019 at 21:06
  • So as I said div is the one you should select. You can use xpath= .//div[@class='recaptcha-checkbox-border']
    – Pratik
    CommentedOct 17, 2019 at 21:09
  • Please read why a screenshot of code is a bad idea. Paste the code and properly format it instead.
    – JeffC
    CommentedOct 17, 2019 at 21:13

1 Answer 1

5

This is a recaptha stuff .. it's not like normal elements in the page

you have to navigate with selenium to the captcha frame .. then you can deal with the checkbox element..

to do that you need first to save the main window handle to be get back to it when you're done with the recaptcha

# save the main window handle mainwindow = browser.current_window_handle # get the recapthca iframe then navigate to it frame = browser.find_element_by_tag_name("iframe") browser.switch_to.frame(frame) # now you can access the checkbox element browser.find_element_by_id("recaptcha-anchor").click() # navigate back to main window browser.switch_to.window(mainwindow) 

for further info about how to deal with the recaptcha challenge check this link

0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.