我正在学习Selenium,并且具有Chrome版本 61.0.3163.100 和ChromeDriver版本 2.34.522940 。我尝试了这段代码:
public void adminLogin(){
driver.findElement(By.name("userId")).sendKeys("sysadmin");
}
我的页面正在加载,但是 username 字段未填充“ sysadmin”。这是我得到的错误:
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)
任何帮助将不胜感激。
答案 0 :(得分:0)
尝试使用Actions
类首先关注元素,然后再使用sendkeys()
,如下所示:
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.name("userId")));
actions.click();
actions.sendKeys("sysadmin");
actions.build().perform();
答案 1 :(得分:0)
面临类似问题。您使用的是chromedriver的过时版本。将chromedriver更新到最新版本,并相应地设置webdriver属性。
WebDriver browser = new ChromeDriver();
String path_to_driver = "C:\\blah\\blah\\chromedriver.exe"
System.setProperty("webdriver.chrome.driver", path_to_driver);
接下来,您将需要将硒库升级到最新版本,即Selenium,这是Chromedriver向后不兼容的问题。
- 最新硒版本为 3.141.59
- 最新的Chromedriver版本为: 73.0.3683.20
ChromeDriver下载链接:Download Chrome Driver
硒下载链接:Download Selenium
注意:
请确保在导航到 预期的URL。原因是,元素会在HTML DOM中填充 一定的时间流逝。如果网络驱动程序在以下情况下遍历DOM 这次,driver.findElement()将无法获取Web元素。
webdriver.get(url);
WebDriverWait wait_time = new WebDriverWait(webdriver, 10);
答案 2 :(得分:0)
此错误消息...
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
...表示 ChromeDriver 无法集中关注所需元素。
您的主要问题是所使用的二进制版本之间的不兼容性:
因此 JDK v8u25 , ChromeDriver v2.34 和 Chrome浏览器v61.0
之间存在明显的不匹配按照最佳做法,
@Test
。driver.quit()
方法内调用tearDown(){}
,以优雅地关闭和销毁 WebDriver 和 Web Client 实例。