当元素属性不存在时,使用 java 在 Selenium WebDriver 中按 ENTER 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33387626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Press ENTER key in Selenium WebDriver with java when element property not present
提问by Santhosh Chandrashekar
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
当我输入 url 时,我收到了需要身份验证的对话框
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 按下 ok 按钮?
回答by Sighil
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.
使用 Selenium 无法直接处理凭证框您可以使用 JAVA AWT 机器人类按 Enter。这个类在 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.
请注意,当您使用这些解决方案时,在运行测试用例时无法锁定工作站屏幕。
回答by LINGS
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.
下面的代码最多等待 10 秒让弹出窗口出现,然后单击“确定”接受它。虽然等待是可选的。
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
回答by Rahul Pandey
Try this code snippet:
试试这个代码片段:
driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);
It will definitely work.
它肯定会起作用。