Selenium alert authentification

China☆狼群 提交于 2019-12-11 05:07:53

问题


I am trying to access a website located on my localhost. When I navigate to it, an alert appears and I have to authenticate, but when I use the method "authenticateUsing", it shows me that it does not exist. Can anyone help me?

        System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("http://localhost:8015/thing/Home");
        String username = "a";
        String password = "bbbbb";
        WebDriverWait wait = new WebDriverWait(driver, 10);      
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
        alert.authenticateUsing(new UserAndPassword(username, password));

The error looks like:

I want to fill an alert like this one:


回答1:


This question does not provide enough information though. I will give you some Approaches :

Approach 1 :

You have to switch to alert box before you want to have interaction.

Alert alert = driver.switchTo().alert();  

Then you can proceed further in your execution. Something like this after switching to alert :

wait.until(ExpectedConditions.alertIsPresent());  
alert.authenticateUsing(new UserAndPassword(username, password)); 

Approach 2 :

String username = "a";
String password = "bbbbb";
String URL = "http://" + username  + ":" + password + "@" + localhost:8015/thing/Home;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();  

Approach 3 : You may try AutoIT script. Please note that AutoIT script will work only in windows environment. However, I would not recommend you to go with this approach.



来源:https://stackoverflow.com/questions/51127659/selenium-alert-authentification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!