简体   繁体   中英

Press ENTER key in Selenium WebDriver with java when element property not present

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?

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();

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.

Try this code snippet:

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

It will definitely work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM