Selenium 无法访问隐藏元素

Selenium cant access a hidden element

我对 Java 中的以下代码感到头疼,试图在实施之前解决我工作中的 selenium 测试的重新验证。

我使用的用例: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);

因此,对于 system.outs,我试图找到获取验证码令牌的方式,但每次它 returns 都是一个空元素。目前SeleniumUtil所做的主要是:

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

By.CssSelector等也是如此

我已经在正确的 IFrame 中,因为页面源已正确打印出来,但通过 #recaptcha-token (cssselector)、recaptcha-token (id)、//*@id="recaptcha-token 进行搜索" 不起作用。

也许有人可以帮我解决这个问题?

存在需要切换驱动的iFrame

经过几次尝试,我得到了:

标准 chromedriver 设置:

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;
    }

}

并获取令牌:

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

我的输出,最后一行是令牌:

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_uCpJJ6aktscF97lGWA7EXJfpuOxaCMRt3VkuDZ9mzA5RFiHnQOX9I8VFWWe03t71r69aoj5y1dNoFUVT29lMsZf-X3y00eUxlLiT4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