首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selenium-4 -无法处理远程驱动程序弹出的HasAuthentication

selenium-4 -无法处理远程驱动程序弹出的HasAuthentication
EN

Stack Overflow用户
提问于 2022-08-09 10:24:12
回答 1查看 192关注 0票数 3

我试图处理自动弹出,并且在本地(当我们用chromeDriver创建webdriver时)很好地工作。但是如果我们尝试使用RemoteWebDriver,我们将得到下面提到的异常。

代码语言:javascript
复制
Our problem is to handle authentication pop up in ci environment using RemoteWebDriver.

请你做同样的指导。

代码语言:javascript
复制
Error:
INFO: Using OpenTelemetry for tracing
Aug 05, 2022 3:44:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected upstream dialect: W3C
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.HasAuthentication
at domain.TestWithRemoteDriver.main(TestWithRemoteDriver.java:47)
代码语言:javascript
复制
public class TestWithRemoteDriver {
    public static void main(String[] args) throws MalformedURLException {
        Boolean isGridEnable = true;
        WebDriver webDriver;
        if (isGridEnable) {
            ChromeOptions chromeOptions = getChromeDesiredCapabilities();
            String seleniumRemoteUrl = "http://localhost:4444/wd/hub";
            webDriver = new RemoteWebDriver(new URL(seleniumRemoteUrl), chromeOptions);
        } else {
          WebDriverManager.getInstance(DriverManagerType.CHROME).setup();
            webDriver = new ChromeDriver();
        }
        ((HasAuthentication) webDriver).register(UsernameAndPassword.of("admin", "admin"));
        webDriver.get("https://the-internet.herokuapp.com/basic_auth");
        String success = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div/p")).getText();
        webDriver.quit();
    }
    private static ChromeOptions getChromeDesiredCapabilities() {
        ChromeOptions chromeOptions = new ChromeOptions();
        HashMap<String, Object> chromePrefs = new HashMap<>();
        chromeOptions.addArguments("--start-fullscreen");
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("--start-maximized");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.setExperimentalOption("prefs", chromePrefs);
        return chromeOptions;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-23 15:53:05

请尝试Selenium 4双向api:

((HasAuthentication) webDriver) .register(() -> new UsernameAndPassword("admin","admin"));

https://saucelabs.com/resources/articles/bidirectional-apis

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73290245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档