ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-Selenium sendKeys()对于Chrome,Firefox和Safari的不同行为

2019-10-10 03:06:01  阅读:14  来源: 互联网

标签:java selenium selenium-webdriver



以下是简单的代码段.

@FindBy(className = "element-to-press")
private WebElement elementToPress;

// some other code

Keys move = Keys.ARROW_DOWN;
gridContainer.sendKeys(move);

在Firefox中,一切正常.

在Chrome浏览器中,我收到“ org.openqa.selenium.WebDriverException:未知错误:无法聚焦元素”错误

Safari不响应sendKeys()

我也尝试使用另一种方法:

Keys move = Keys.ARROW_DOWN;
Actions actions = new Actions(driver);
actions.moveToElement(element).click(); // to focus on element
actions.sendKeys(move).perform();

此代码修复了Chrome浏览器“无法聚焦元素”错误,但浏览器仍无法对Keys.ARROW_DOWN / Keys.ARROW_UP / Keys.ARROW_LEFT / Keys.ARROW_RIGHT事件做出反应

Safari和Firefox也不会对sendKeys()做出反应.

我不知道发生了什么以及如何使我的代码在所有这些浏览器上运行.

环境:

> OS X 10.9.5
>最新版本的Chrome / Safari / Firefox
>硒版本2.44.0

UPD:

将Selenium更新到2.46版.

现在适用于Chrome和Firefox.

但是Safari仍然存在问题:org.openqa.selenium.remote.UnreachableBrowserException:无法启动新的会话.

UPD2:

我安装了Safari浏览器扩展程序.

解决方法:

如聊天中所讨论的,您想自动化2048

解决方法是我写了Javascript based on this发送Arrow KeyEvents,并且它在所有浏览器上都有效

您要做的就是发送箭头键,根本不需要单击元素[和查找元素,除非您创建一个基于值播放的算法]

你让我明白了以下内容将为3个浏览器玩200(50 x 4)种不同动作的游戏

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Main {

public static void main(String args[]) {
    play(new FirefoxDriver());
    play(new SafariDriver());
    System.setProperty("webdriver.chrome.driver", "/Users/Apple/Documents/chromedriver");
    play(new ChromeDriver());
}

public static void play(WebDriver driver) {
    driver.get("http://gabrielecirulli.github.io/2048");
    driver.findElement(By.className("restart-button")).click();
    String script = "fireKey(arguments[0]);\n"
            + "function fireKey(arrow)\n"
            + "{\n"
            + "    var key;\n"
            + "    switch (arrow.toLowerCase())\n"
            + "    {\n"
            + "        case \"left\":\n"
            + "            key = 37;\n"
            + "            break;\n"
            + "        case \"right\":\n"
            + "            key = 39;\n"
            + "            break;\n"
            + "        case \"up\":\n"
            + "            key = 38;\n"
            + "            break;\n"
            + "        case \"down\":\n"
            + "            key = 40;\n"
            + "            break;\n"
            + "    }\n"
            + "    if (document.createEventObject)\n"
            + "    {\n"
            + "        var eventObj = document.createEventObject();\n"
            + "        eventObj.keyCode = key;\n"
            + "        document.documentElement.fireEvent(\"onkeydown\", eventObj);\n"
            + "    } else if (document.createEvent)\n"
            + "    {\n"
            + "        var eventObj = document.createEvent(\"Events\");\n"
            + "        eventObj.initEvent(\"keydown\", true, true);\n"
            + "        eventObj.which = key;\n"
            + "        document.documentElement.dispatchEvent(eventObj);\n"
            + "    }\n"
            + "} ";

    JavascriptExecutor js = (JavascriptExecutor) driver;
    //use left,right,up,down for arrow keys
    for (int i = 0; i < 50; i++) {
        js.executeScript(script, "left");
        js.executeScript(script, "right");
        js.executeScript(script, "up");
        js.executeScript(script, "down");
    }
    System.out.println("Score :" + driver.findElement(By.className("score-container")).getText());
    driver.quit();
}
}


标签:java,selenium,selenium-webdriver

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有