8

Why am I getting errors when trying to get the driver to click on the reCAPTCHA button?

This is the site where I am trying to get it to work: https://rsps100.com/vote/760/

This is my current code so far:

WebElement iframeSwitch = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div/div/div[2]/div/form/div/div/div/div/iframe"));
driver.switchTo().frame(iframeSwitch);
driver.findElement(By.cssSelector("div[class=recaptcha-checkbox-checkmark]")).click();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
alqqu
  • 111
  • 1
  • 1
  • 8

5 Answers5

13

To invoke click() on the reCaptcha checkbox as the element is within an <iframe> you need to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use the following solution:

    • Code Block:

      public class ReCaptcha_click {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("start-maximized");
              options.addArguments("disable-infobars");
              options.addArguments("--disable-extensions");
              WebDriver driver = new ChromeDriver(options);
              driver.get("https://rsps100.com/vote/760");
              new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")));
              new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
          }
      }
      
  • Browser Snapshot:

    reCaptcha

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    :You always steal our efforts :).I have run the code 10 times and it is working fine but OP complaing. – KunduK Mar 20 '19 at 16:05
  • @KajalKundu I would love to see you succeed but yes following the best practices so the new contributors are guided in the best possible manner :) still your contributions are very helpful. – undetected Selenium Mar 20 '19 at 16:07
  • SO is really confusing when you provide entire code other contributor always complaining about that.When you give specific answer OP also confuse :) – KunduK Mar 20 '19 at 16:11
  • 3
    for my case, I had to click on 'div.rc-anchor-content' element – gtiwari333 Jan 25 '20 at 03:33
  • Any tips on getting through the next part? Selecting correct images etc? @KunduK – Phil Brockwell Apr 17 '21 at 15:02
  • As mentioned in the comments I also needed 'div.rc-anchor-content' but also the following: "//iframe[starts-with(@title, 'reCAPTCHA') and starts-with(@src, 'https://recaptcha.net')]" – Toofy Jul 14 '21 at 22:02
3

Use WebDriverWait to identify the element.See if this help.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-')]")));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark")));
element.click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • is my xpath right? is my cssSelector thing right? i tried with your code and my code combination --> https://pastebin.com/g1pfzduq but it said "could not find element" and a lot of red errors in console – alqqu Mar 20 '19 at 15:47
  • have you tried yourself? i got this error --- xpected condition failed: waiting for frame to be available: By.xpath: //iframe[starts-with(@name,'a-')] (tried for 30 second(s) with 500 milliseconds interval) --- caused by: no such element – alqqu Mar 20 '19 at 15:56
  • your frame is dynamic so i have written xpath like this.Copy the entire code with frame switching – KunduK Mar 20 '19 at 15:58
3

This worked for me. Please note that I am using Selenide. For regular selenium code look the same.

    import static com.codeborne.selenide.Selenide.*;

    void recaptchaTest() {

        open("https://rsps100.com/vote/760");

        switchTo().frame($x("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"));

        $("div.rc-anchor-content").click();

        switchTo().defaultContent();

    }
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
0

Here is the code that should work.

driver.switchTo().frame("a-9wt0e8vkopnm");
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • 1
    Exception in thread "main" org.openqa.selenium.NoSuchFrameException: No frame element found by name or id a-9wt0e8vkopnm, is what i got when i tried your code. yes, i let the website load correctly until i executed these commands but still got the error – alqqu Mar 20 '19 at 15:53
  • I think that's right name, but can you try with ID. – supputuri Mar 20 '19 at 16:04
0

I solved this maybe on the stupid way. But hawe in mind that I am not in test environment and I practicing automatization of tests. So this is my solution:

Beside using

public void notABot( ) {

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds( 15 ));
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-') and starts-with (@src, 'https://www.google.com/recaptcha')]")));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div [ @class =  'recaptcha-checkbox-border']"))).click( );
    driver.switchTo().defaultContent();
}

this, I also added custom send keys method

public void inputEmail( ) {

    inputEmail.click( );

    String email = Strings.EMAIL_FOR_SIGNUP;
    for (int i = 0; i < email.length(); i++) {

        char c = email.charAt(i);
        String s = new StringBuilder( ).append( c ).toString( );

        inputEmail.sendKeys( s );
        sleepSendKeys( );
    }
}

Sleep is 300 millis. In 96 procent time I manage to cheat google reCaptcha that actually human is login to the page. Its work for me

Milan
  • 1
  • 1