[英]org.openqa.selenium.WebDriverException: unknown error: cannot focus element with ChromeDriver Chrome and Selenium through Java
I am learning Selenium, and I have Chrome version 61.0.3163.100 and ChromeDriver version 2.34.522940 . 我正在学习Selenium,并且具有Chrome版本61.0.3163.100和ChromeDriver版本2.34.522940 。 I tried this code: 我尝试了这段代码:
public void adminLogin(){
driver.findElement(By.name("userId")).sendKeys("sysadmin");
}
My page is getting loaded, but the username field is not getting populated with "sysadmin". 我的页面正在加载,但是用户名字段未填充“ sysadmin”。 This is the error I am getting: 这是我得到的错误:
Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 31275
Only local connections are allowed.
Feb 27, 2019 11:16:44 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'DWA7DEVOS00170', ip: '10.96.162.167', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.34.522940 (1a76f96f66e3ca..., userDataDir: C:\Users\rajos1\AppData\Loc...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 61.0.3163.100, webStorageEnabled: true}
Session ID: ff3cf91166eddfe1667f931b18ede2ea
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:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:100)
at pulse.tpr.LoginInterface.adminLogin(LoginInterface.java:24)
at pulse.tpr.LoginInterface.main(LoginInterface.java:34)
Any help would be appreciated. 任何帮助,将不胜感激。
Try using Actions
class to first focus on element before using sendkeys()
as shown below: 尝试使用Actions
类首先关注元素,然后再使用sendkeys()
,如下所示:
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.name("userId")));
actions.click();
actions.sendKeys("sysadmin");
actions.build().perform();
Faced similar issue. 面临类似的问题。 You are using an outdated version of chromedriver. 您使用的是chromedriver的过时版本。 Update the chromedriver to the latest version and set the webdriver property accordingly. 将chromedriver更新到最新版本,并相应地设置webdriver属性。
WebDriver browser = new ChromeDriver();
String path_to_driver = "C:\\blah\\blah\\chromedriver.exe"
System.setProperty("webdriver.chrome.driver", path_to_driver);
Next, you will be needing to upgrade your selenium library to the latest version as Selenium as backward incompatibility issues with Chromedriver. 接下来,您将需要将Selenium库升级为Selenium的最新版本,这是Chromedriver向后不兼容的问题。
- Latest Selenium version is 3.141.59 Selenium的最新版本是3.141.59
- Latest Chromedriver version is: 73.0.3683.20 最新的Chromedriver版本是: 73.0.3683.20
ChromeDriver download link: Download Chrome Driver ChromeDriver下载链接: 下载Chrome驱动程序
Selenium download link: Download Selenium 硒下载链接: 下载硒
Note: 注意:
Make sure you wait for some time after you navigate to the intended URL. 导航到所需的URL后,请确保等待一段时间。 Reason being, elements get populated in HTML DOM after a certain time lapse. 原因是,经过一段时间后,元素将填充到HTML DOM中。 In case, the webdriver traverses the DOM during this time, the driver.findElement() will fail to fetch the web element. 万一在此期间,Web驱动程序遍历DOM,driver.findElement()将无法获取Web元素。
webdriver.get(url);
WebDriverWait wait_time = new WebDriverWait(webdriver, 10);
This error message... 此错误消息...
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'DWA7DEVOS00170', ip: '10.96.162.167', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver
...implies that the ChromeDriver was unable to focus on the desired element . ...表示ChromeDriver无法将焦点放在所需的元素上 。
Your main issue is the incompatibility between the version of the binaries you are using as follows: 您的主要问题是所使用的二进制版本之间的不兼容性 ,如下所示:
So there is a clear mismatch between the JDK v8u25 , ChromeDriver v2.34 and the Chrome Browser v61.0 因此, JDK v8u25 , ChromeDriver v2.34和Chrome浏览器v61.0之间存在明显的不匹配
As per best practices, 根据最佳做法,
@Test
. 执行您的@Test
。 driver.quit()
within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully. 始终在tearDown(){}
方法内调用driver.quit()
,以优雅地关闭和破坏WebDriver和Web Client实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.