Search code examples
javaseleniumselenium-webdriverrecaptchacaptcha

Selenium cant access a hidden element


breaking my head over the following piece of code in Java, trying to solve a recaptcha for my selenium tests at work before implementing them.

The use case I use: https://www.google.com/recaptcha/api2/demo

    WebDriver driver = Selenium.getInstance();
    Selenium.goToWebPage("https://www.google.com/recaptcha/api2/demo");

    WebDriverWait wait = new WebDriverWait(driver, 5);
    List<WebElement> allIFrames = SeleniumUtil.getWebElementNicelyByTagName(driver, wait,   "iframe");
    if(allIFrames == null){
        return;
    }
    Optional<WebElement> captchaIFrame = allIFrames.stream().filter(webElement -> webElement.getAttribute("src").contains("recaptcha/api2/anchor")).findFirst();
    if(!captchaIFrame.isPresent()){
        return;
    }
    WebDriver frame = driver.switchTo().frame(captchaIFrame.get());

    //System.out.println(frame.getPageSource());

    WebElement recaptchaTokenElement = SeleniumUtil.getWebElementNicelyByXPath(frame, wait, "//*[@type='hidden']");
    System.out.println(recaptchaTokenElement);

So with the system.outs i'm trying to find my way to the captcha token, but every time it returns a null element. What the SeleniumUtil so far does is mainly:

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
        return driver.findElement(By.xpath(xpath));

And the same for By.CssSelector etc.

I am already in the right IFrame, because the page source is printen out correctly, but searching by #recaptcha-token (cssselector), recaptcha-token (id), //*@id="recaptcha-token" does not work.

Maybe someone can help me with this?


Solution

  • There is a iFrame where the driver needs to be switched to.

    After few tries I've got:

    standard chromedriver setup:

    package selenium;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class WebDriverSetup {
    
        public static String userDir = System.getProperty("user.dir");
    
        public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";  
        public static WebDriver driver;
        
        public static WebDriver startChromeDriver() {
            System.setProperty("webdriver.chrome.driver", chromedriverPath);
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            driver = new ChromeDriver(options);
            driver.manage().window().maximize();
            return driver;
        }
    
    }
    

    And getting the token:

    package selenium;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class GoogleRecaptchaDemo extends WebDriverSetup {
    
        public static void main(String[] args) {
            WebDriver driver = startChromeDriver();
            driver.get("https://www.google.com/recaptcha/api2/demo");
            List<WebElement> frames = driver.findElements(By.tagName("iframe"));
            driver.switchTo().frame(frames.get(0));
            WebElement rc = driver.findElement(By.id("recaptcha-anchor-label"));
            rc.click();
            WebElement recaptchaToken = driver.findElement(By.id("recaptcha-token"));
            String token = recaptchaToken.getAttribute("value");
            System.out.println(token);
            driver.quit();
        }
    }
    

    My output, last line is the token:

    Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 25872
    Only local connections are allowed.
    Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
    ChromeDriver was started successfully.
    Pro 01, 2020 4:30:45 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    03AGdBq24MumCzjDlQCrcb4N3BuIiHm6O7V5IKSNGP1wChYe9g1Vc1GR7_NsvKqKV1Jzb5yLUXCB-ko8satP3fgkXIZ1rbjz76FITaLRRZqRJteQdVzlyzYoUgsSWQ_xjCspDvKNl6uTRGP7EzhesblTBf7HyYmjDU26BZkji8NqMFypQUzUIWUE6CF1W_UeiGCwV4Cfr2UduHJPskDubiwT7jxhRrLKFF_nK8zruV9dStwynSGzrMjwtemZSZmj2ZVJPiTxiYIV2QRIPUYPQ7dXMezrqJDG2Vwn2M9AxHOacoFcBTDIiFYoENq11-jNDJTY0a8PWl6bixpqv1itwTJw4FYnz9vSAQUCztQf7WXPLFgxp8iHjqB-uSIY39ikLJfa_rkFOke90PkF7sRlsKoQjptp_uCpJJ6aktscF97lGWA7EXJfpuOxaCMRt3VkuDZ9mzA5RFiHnQOX9I8VFWWe03t71r69ao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