我在这里打开Chrome浏览器并尝试将输入字符串发送到WebElement搜索文本框:
// search for a text in Google
WebDriverWait wait3 = new WebDriverWait(driver1,30);
WebElement textBox = wait3.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class = 'gsfi']")));
Chrome观察到的错误
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
System info: host: 'ADMIN-PC', ip: '192.168.1.5', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\admin\AppData\Local\Temp\scoped_dir15624_26281}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=62.0.3202.94, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: d4178e5e05d8efc8800ec1e1ceb1f63a
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:82)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
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:586)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:100)
at packageFF.browserAutomation.main(browserAutomation.java:76)
答案 0 :(得分:0)
By.xpath("//input[@class = 'gsfi']")
,因为它未成功打开。您也可以设置睡眠时间。
如果可能,您可以使用id或class而不是xpath。
答案 1 :(得分:0)
您正在搜索具有“gsfi
”类的输入元素。通过Google主页html快速搜索会返回三个具有此类的输入元素,其中两个是隐藏的。您需要发送文本的字段具有ID,所以为什么不使用id?
// The input element you need
<input class="gsfi" id="lst-ib" .../>
// Locate element by id
WebDriverWait wait = new WebDriverWait(driver1, 30);
WebElement textBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("list-ib")));