我希望加载2048 game,然后单击 向下 键。 我已经尝试了以下代码:
driver=new ChromeDriver();
driver.get("http://2048game.com");
String path="/html/body/div[2]/div[3]/div[3]/div[1]/div";
driver.findElement(By.xpath(path)).sendKeys(Keys.ARROW_DOWN);
path
是最左上方的图块的xpath。
也尝试了.sendKeys(Keys.DOWN);
,但也没有帮助。
它成功加载了页面,但是它没有按 DownKey
?
我在控制台中的代码抱怨:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 38 milliseconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'LAPTOP-BD6AOVKQ', ip: '192.168.1.208', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\salman\AppData\Local\Temp\scoped_dir23108_15057}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=69.0.3497.100, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 6be1718d9dbeddfa7508d895eb40a10c
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:96)
at services.Hook.<init>(Hook.java:27)
at mains.Online2048.main(Online2048.java:9)
答案 0 :(得分:0)
我在firefox浏览器上尝试过相同的代码。请看看,它正在工作。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class geckodriver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\XXXX\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
Thread.sleep(5000);
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// capabilities.setCapability("marionette", true);
//
// WebDriver driver = new FirefoxDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("your firefox version");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(capabilities);
// driver.get("http://www.google.com");
driver.get("http://2048game.com");
String path="/html/body/div[2]/div[3]/div[3]/div[1]/div";
driver.findElement(By.xpath(path)).sendKeys(Keys.ARROW_DOWN);
System.out.println("Done");
Thread.sleep(5000);
driver.quit();
}}
答案 1 :(得分:0)
根据错误消息,您尝试向其应用sendKeys的元素未聚焦-因此ChromeDriver无法与其交互。
如果您只想按向下箭头,则以下代码应该起作用(只需更改示例中的xPath):
driver=new ChromeDriver();
driver.get("http://2048game.com");
String path="//*";
driver.findElement(By.xpath(path)).sendKeys(Keys.ARROW_DOWN);
答案 2 :(得分:0)
此错误消息...
org.openqa.selenium.WebDriverException: unknown error: cannot focus element
...表示 ChromeDriver 无法集中关注所需的 WebElement 。
您的主要问题是所使用的二进制版本之间的不兼容性:
支持 Chrome v58-60
支持 Chrome v68-70
因此 JDK v8u171 , Selenium Client v3.4.0 , ChromeDriver v2.36 和 Chrome浏览器v69.0
@Test
。答案 3 :(得分:0)
使用chromeDriver找到了解决方案。
driver.get("http://2048game.com");
WebElement e1=driver.findElement(By.xpath("/html[1]/body[1]"));
e1.sendKeys(Keys.ARROW_DOWN);
System.out.println("Done");
Thread.sleep(15000);
driver.quit();
答案 4 :(得分:0)
在向其发送键之前,需要将焦点设置在该元素上。
为了修复它,我们可以使用Selenium Actions:
Actions assigner = new Actions(webdriver);
assigner.moveToElement(webelement);
assigner.click();
assigner.sendKeys("<your value>");
assigner.build().perform();