Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). But if we try with RemoteWebDriver, we are getting below mentioned exception.

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;
    }
}
like image 825
Pavan Washimkar Avatar asked Dec 20 '25 16:12

Pavan Washimkar


1 Answers

Please try Selenium 4 bi directional api:

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

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

like image 94
Sneh Tripathi Avatar answered Dec 23 '25 07:12

Sneh Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!