简体   繁体   English

selenium-4 - 无法使用远程驱动程序处理 HasAuthentication 弹出窗口

[英]selenium-4 - Not able to handle HasAuthentication popup with Remote driver

I was trying to handle authetication popup and is working fine in local (when we create webdriver instance with chromeDriver).我试图处理身份验证弹出窗口并且在本地工作正常(当我们使用 chromeDriver 创建 webdriver 实例时)。 But if we try with RemoteWebDriver, we are getting below mentioned exception.但是如果我们尝试使用 RemoteWebDriver,我们会得到下面提到的异常。

Our problem is to handle authentication pop up in ci environment using RemoteWebDriver.

Request you to guide for the same.请您指导。

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

Please try Selenium 4 bi directional api:请尝试 Selenium 4 双向 api:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM