简体   繁体   English

当元素属性不存在时,使用java在Selenium WebDriver中按ENTER键

[英]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.

相关问题 如何使用Java在Selenium WebDriver中按“TAB”然后按“ENTER”键? - How to press “TAB” then “ENTER” key in Selenium WebDriver using Java? 使用 java 在 Selenium WebDriver 中按下(Ctrl + 鼠标单击) - Key press in (Ctrl + mouse click) in Selenium WebDriver using java 在Selenium脚本中按Enter键 - Press Enter key in Selenium script 如何使用Java和Selenium WebDriver识别元素中的方括号是否存在 - How to identify if brackets in the element are present or not using Java and Selenium WebDriver 使用带有 Java 的 Selenium WebDriver 检查元素不存在的最佳方法 - Best way to check that element is not present using Selenium WebDriver with java 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse JAVA:当选择了jTextField时,在“ enter”按键上激活jFrameButton - JAVA: Activating a jFrameButton on “enter” key press when a jTextField is selected 模拟使用Selenium在Webtest中输入'按键' - mimic 'Enter' Key press in Webtest using Selenium 在硒中按电话键盘输入键 - press enter key from phone keyboard in selenium Selenium WebDriver:当使用WebDriver.findElement定位时,等待元素存在是不可能的 - Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM