alqqu
alqqu

Reputation: 111

How to click on the reCAPTCHA using Selenium and Java

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();

Upvotes: 8

Views: 45999

Answers (5)

Milan
Milan

Reputation: 1

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

Upvotes: 0

gtiwari333
gtiwari333

Reputation: 25146

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();

    }

Upvotes: 4

KunduK
KunduK

Reputation: 33384

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();

Upvotes: 3

undetected Selenium
undetected Selenium

Reputation: 193058

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

Upvotes: 13

supputuri
supputuri

Reputation: 14135

Here is the code that should work.

driver.switchTo().frame("a-9wt0e8vkopnm");
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();

Upvotes: 0

Related Questions