[英]Press ENTER key in Selenium WebDriver with java when element property not present
I am using selenium webdriver with Java to automate the webpages 我正在使用带有Java的selenium webdriver来自动化网页
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 注意:无法获取ok按钮属性,因此无法使用以下代码
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Is there any other way to press on ok button through webdriver? 有没有其他方法通过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. 下面的代码最多等待10秒钟,以便弹出窗口出现,然后单击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. 使用Selenium无法直接处理凭证框您可以使用JAVA AWT机器人类按Enter键。 This class is available in the java API itself.
这个类在java API本身中可用。
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 . 或者,您可以使用AutoIt或基于图像的测试工具,如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. 它肯定会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.