【问题标题】:Press "enter" key on Selenium (Java/chrome) [duplicate]在 Selenium(Java/chrome)上按“输入”键 [重复]
【发布时间】:2015-09-09 18:11:02
【问题描述】:

我使用 xpath 单击一个 web 元素,然后弹出一个确认窗口。我只需要在 selenium 上按 enter 。我试过这个:

WebElement.sendKeys(Keys.RETURN);

我从WebElement 类型中得到了这个错误:

无法对非静态方法sendKeys(CharSequence...)进行静态引用

【问题讨论】:

  • 这与 Selenium 并没有什么特别的关系。您是否通过 Google 搜索该错误以努力了解它是什么、为什么会发生以及是否有类似的问题以及您可以使用的解决方案?

标签: java selenium xpath key enter


【解决方案1】:

为了在弹出时按下回车键,首先使用getWindowHandles()切换到弹出,然后使用Actions()接口传入回车键。这是怎么做的-

String subWindowHandler = null;
Actions action = new Actions(webDriverInstance);
Set<String> windowHandler = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = windowHandler.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); //switch to pop up
//Perform your actions on the pop up
action.sendKeys(Keys.RETURN).perform(); //press enter key

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 2023-01-19
    • 1970-01-01
    • 2021-01-04
    • 2015-05-16
    • 2018-08-05
    • 2023-01-11
    • 2011-09-04
    相关资源
    最近更新 更多