java - Selenium sendKeys() different behaviour for Chrome, Firefox and Safari -


below simple snippet of code.

@findby(classname = "element-to-press") private webelement elementtopress;  // other code  keys move = keys.arrow_down; gridcontainer.sendkeys(move); 

in firefox works fine.

in chrome i've got "org.openqa.selenium.webdriverexception: unknown error: cannot focus element" error

safari not react sendkeys()

also i've tried use approach:

keys move = keys.arrow_down; actions actions = new actions(driver); actions.movetoelement(element).click(); // focus on element actions.sendkeys(move).perform(); 

this code fixed chrome "cannot focus element" error, browser still not react keys.arrow_down/keys.arrow_up/keys.arrow_left/keys.arrow_right events

safari , firefox not react sendkeys() well.

i cannot understand going on , how can make code run browsers.

environment:

  • os x 10.9.5

  • latest version of chrome/safari/firefox

  • selenium version 2.44.0

upd:

update selenium version 2.46.

works chrome , firefox now.

but still have issue safari:org.openqa.selenium.remote.unreachablebrowserexception: not start new session.

upd2:

i have safari browser extension installed. enter image description here

as discussed in chat want automate 2048

as workaround i've written javascript based on this send arrow keyevents.and worked on browsers

all want send arrow keys no need click element @ [and find element ,unless create algorithm play based on values)

you got me this.the following play game 200(50 x 4) different moves 3 browsers

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 arrow keys     (int = 0; < 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(); } } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -