2

I am using selenium webdriver with Java to automate the webpages

When I enter the url, I am getting the authentication required dialog box

I am able to enter the username and password by configuring profile but I am not able to click on OK button

Note: Not able to get the ok button property so am not able to use the below code

import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);

Is there any other way to press on ok button through webdriver?

Tunaki
  • 132,869
  • 46
  • 340
  • 423

3 Answers3

5

You need to handle it as an alert box, wait for popup to appear and click OK.

Below code waits up to a maximum of 10 seconds for the popup to be present and then accepts it by clicking OK. Although wait is optional.

new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
LINGS
  • 3,560
  • 5
  • 34
  • 47
4

Handling credential boxes is not possible directly using Selenium You can use the JAVA AWT robot class to press enter. This class is available in the java API itself.

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

Alternatively, you can use AutoIt or an image based testing tool like SIKULI http://www.sikuli.org.

Please note that when you are using these solutions, the workstation screen cannot be locked while running the test cases.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
4

Try this code snippet:

driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);

It will definitely work.

Bugs
  • 4,491
  • 9
  • 32
  • 41