Selenium3.0 测试百度网页运行出问题求解答
package com.example.tests;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
public class Demo {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\webdriver\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
System.out.println("Page title is :"+driver.getTitle());
//WebElement username=driver.findElement(By.xpath("//form/div/input[@id='lgn_txt_username']"));
WebElement Text=driver.findElement(By.xpath("html/body/div/div/div/div/div/form/span/input"));
Text.clear();
Text.sendKeys("abc");
//username.sendKeys("admin");
}
}
只想在百度输入框内 输入abc但是Eclipse运行此断代码就报错。
只能打开百度页面不能执行输入操作。
下面试Eclipse中运行Console中的信息
1497512421381 geckodriver INFO Listening on 127.0.0.1:30863
六月 15, 2017 3:40:21 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1497512421978 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.PKQFmN5A8CRE
1497512421982 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1497512421990 geckodriver::marionette INFO Connecting to Marionette on localhost:63668
1497512424910 Marionette INFO Listening on port 63668
WARNING: pipe error: 109: file c:/builds/moz2_slave/m-rel-w32-00000000000000000000/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
六月 15, 2017 3:40:25 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
Page title is :百度一下,?就知道
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Expected undefined to be a string
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: '13K2G_wangchunpeng', ip: '10.241.36.187', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.PKQFmN5A8CRE, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0.3, platformVersion=10.0, moz:processID=520, browserName=firefox, platformName=windows_nt}]
Session ID: 24f372f7-6351-4d8b-9c2f-91e082353358
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:98)
at com.example.tests.Demo.main(Demo.java:19)
它说object undefined 看看‘’Text‘’中的内容对不对,判断下获取的元素对不对 在输入文本之前,加一个时间等待试一下 cq520131517 发表于 2017-6-15 19:56
在输入文本之前,加一个时间等待试一下
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS); 是加这句吗?
添加完了 但问题还发生。 finomy 发表于 2017-6-15 16:22
它说object undefined 看看‘’Text‘’中的内容对不对,判断下获取的元素对不对
害怕因为定位不对会有问题。 所以Xpath上没有更改。直接firepath获取下来的路径。
感谢大家的积极回复。问题已经解决。
解决办法:
Selenium由之前的3.0.1更新为3.4.0
Firefor的驱动有geckodriver由之前的0.13.0更新到0.17.0
更新以后上述问题不再发生。
看selenium日志,导致问题的原因是geckodriver上的问题。
当前环境下正常运行:
Selenium3.4.0
geckodriver0.17.0
FireFox53.0.3(x86)
OS:Win 10 x64 CN
Eclipse4.6
环境问题有时真的很头疼
页:
[1]