![](/img/trans.png)
[英]Java Selenium error: NoClassDefFoundError: org/openqa/selenium/HasAuthentication
[英]selenium-4 - Not able to handle HasAuthentication popup with Remote driver
我試圖處理身份驗證彈出窗口並且在本地工作正常(當我們使用 chromeDriver 創建 webdriver 實例時)。 但是如果我們嘗試使用 RemoteWebDriver,我們會得到下面提到的異常。
Our problem is to handle authentication pop up in ci environment using RemoteWebDriver.
請您指導。
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;
}
}
請嘗試 Selenium 4 雙向 api:
((HasAuthentication) webDriver).register(() -> new UsernameAndPassword("admin", "admin"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.