转义键在带有 Java 的 Selenium WebDriver 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20263113/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:40:18  来源:igfitidea点击:

Escape key is not working in Selenium WebDriver with Java

javaseleniumselenium-webdriver

提问by Java Beginner

I am Using Selenium WebDriver framework. I have a scenario where a button gets clicked after the textbox get populated and onblur of the textbox.

我正在使用 Selenium WebDriver 框架。我有一个场景,在文本框填充和文本框模糊后单击按钮。

Below is the code which I used for escape sequence which enable the button after filling in textbox.The button get enabled only when the textbox gets filled and when the focus moves out of textbox.

下面是我用于转义序列的代码,它在填充文本框后启用按钮。只有当文本框被填充并且焦​​点移出文本框时,按钮才会启用。

WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8081/TestAutomation/Escape.jsp");
driver.manage().window().maximize();

WebElement txtBxHandle = driver.findElement(By.name("txtName"));        
txtBxHandle.sendKeys("Socrates");

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE);

WebElement BnEnable = driver.findElement(By.name("btnSubmit"));
BnEnable.click();

The above code is not working.I tried keyPressNative but in vain.

上面的代码不起作用。我尝试了 keyPressNative 但没有成功。

Thanks for the Help.

谢谢您的帮助。

回答by Ievgen

You can try to change

你可以尝试改变

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE);

to

txtBxHandle.sendKeys(Keys.ESCAPE);

回答by Louis

Based on my experience with the Python bindings, you'd have to call .perform()on your action chain. I see the Java bindings have the same method. So:

根据我在 Python 绑定方面的经验,您必须调用.perform()您的操作链。我看到 Java 绑定具有相同的方法。所以:

action.sendKeys(Keys.ESCAPE).perform();

回答by Ripon Al Wasim

I have 3 concepts to do this.

我有 3 个概念来做到这一点。

1) You can focus on Submit button to lose focus from text box by using JS with Selenium WebDriver. The code is as below:

1) 您可以通过将 JS 与 Selenium WebDriver 结合使用,专注于提交按钮以从文本框中失去焦点。代码如下:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('btnSubmit')[0].focus();");

2) You can use Actions class as below:

2) 您可以使用 Actions 类,如下所示:

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE).build().perform();

3) You can try the following also:

3)您也可以尝试以下方法:

txtBxHandle.sendKeys(Keys.ESCAPE);

回答by Akash Soori

import java.awt.Robot;
import java.awt.event.KeyEvent;

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);

回答by Ali Shambor

try this

尝试这个

SendKeys.SendWait("{ESC}");

SendKeys.SendWait("{ESC}");