硒无法访问隐藏的元素

先知2000

在Java的以下代码中打破了我的头脑,尝试在实现硒测试的过程中解决该问题。

我使用的用例: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我试图找到通往验证码令牌的方法,但是每次它返回一个null元素时。到目前为止,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-X3y00eUxlLiT4JZzRFPfbF1fd4bqdH3SzpvWROxHe-IGRFz7Z_C4Hf7oNCQSXfpus2DIm2INW-JRPgfqZ_XQTaxphinGcoJ3EMvWZo6a3qDFYUB06HduULNO_5GwZ_eHUxE1wvXxGHdq2uoJ4Bkzzfu01ZD2Q13gJ6Mslup5dZlqAQGNggJY9aJYbmnRKNnUCtSY5Y8xHMr3Lnt_07SuFc_UdEDSV_KdtwgpVWEFL8OgAFP_KZaFqdaeUq_CTnHpHiTcAhNosT7V_H4gKx0A4BgHnI5NVxsLU1p09OOZ0GLJMqeP174pl03EpQy9iqnv0_e-w7NUeO7ZFgETt_pvEbqR6VqjfDO35BNd4aItzer9IdiSfzjdIAR6c0Z3_36o1WQyLXQZv_xTuZXaRYoZUTtV38aG3z5uK-pyRnNDqMZGffy5DPUn4WrDMnPM9lfI5nRZwXS503KJas54w4d0fRrZWOyhxYWeiCYhFJMDDwpqHJAzDlIqwPbQX90vVum0zEaJd_TU0l9t5QioUAwb3BuEdTeaOI8gM2OcpBScsQYTioMivi8-eRYpPvJIP239DJz6Rp5Ptk_52kC4_2rAWuG_cocuco_cA61yE3yu2WCw_e1EauOM3Ug2nGeFKFxZZdGWPdNj0E3YA3N6f3IaJYWHrQAz4odFQZaD-HZ4qnRZlJlnMzV31_o3_NLl-_2CGSywE8kuJf8wAIIplncfDdHSIlUn7WhrIOrSOX83NdKaQYAlTqLI9CTSTjaA3AyhG5OcF2dukVeACOItczNBSJcZeenTMCo1Es986nt770A3tQYC7S9r2ESxlYJl-eWGo8SKuM9udx7ZuO5lKgW-V-EUg1I-YFSoguRmGzUK-liu2-afTBn4X-97VXuiv-tdcKdi9Bx4DtlhqzNGvVbQmIOND

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章