Selenium alert authentification

2019-07-27 08:55发布

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: enter image description here

I want to fill an alert like this one: enter image description here

1条回答
We Are One
2楼-- · 2019-07-27 09:55

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.

查看更多
登录 后发表回答