gpt4 book ai didi

java - org.openqa.selenium.WebDriverException : unknown error: cannot focus element using ChromeDriver Selenium and Java

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:14 25 4
gpt4 key购买 nike

我希望加载 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 是最左上图 block 的 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)

最佳答案

我在 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();
}}

关于java - org.openqa.selenium.WebDriverException : unknown error: cannot focus element using ChromeDriver Selenium and Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com