使用Java在Selenium WebDriver中无法使用Escape键

Jav*_*ner 3 java selenium selenium-webdriver

我正在使用Selenium WebDriver框架.我有一个场景,在填充文本框和文本框的onblur之后点击一个按钮.

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

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();
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用.我试过keyPressNative但是徒劳无功.

谢谢您的帮助.

Lou*_*uis 11

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

action.sendKeys(Keys.ESCAPE).perform();
Run Code Online (Sandbox Code Playgroud)


Rip*_*sim 6

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

1)通过将JS与Selenium WebDriver结合使用,可以将焦点放在“提交”按钮上以使文本框失去焦点。代码如下:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('btnSubmit')[0].focus();");
Run Code Online (Sandbox Code Playgroud)

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

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE).build().perform();
Run Code Online (Sandbox Code Playgroud)

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

txtBxHandle.sendKeys(Keys.ESCAPE);
Run Code Online (Sandbox Code Playgroud)