刚刚开通博客,记录一下学习历程和至今没有解决的问题。
1.这是我开始学习selenium webdriver写的第一段代码
package PRMselenium.pr; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { WebDriver driver=new FirefoxDriver(); driver.get("http://www.baidu.com/"); System.out.println("第一个页面的标题是: " + driver.getTitle()); } }
然后就报错了
2.发现是找不到Firefox的路径,下面是改进的代码
package PRMselenium.pr; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { System.setProperty("webdriver.firefox.bin", "D:\Program Files (x86)\Mozilla Firefox\firefox.exe"); WebDriver driver=new FirefoxDriver(); driver.get("http://www.baidu.com/"); System.out.println("第一个页面的标题是: " + driver.getTitle()); } }
结果还是报错,我就去这个网站下载了一个geckodriver.exe,就是报错里面提示的网址https://github.com/mozilla/geckodriver/releases
3.第二次修改之后的代码
package PRMselenium.pr; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { System.setProperty("webdriver.firefox.driver", "D:\Program Files (x86)\Mozilla Firefox\geckodriver.exe"); System.setProperty("webdriver.firefox.bin", "D:\Program Files (x86)\Mozilla Firefox\firefox.exe"); WebDriver driver=new FirefoxDriver(); driver.get("http://www.baidu.com/"); System.out.println("第一个页面的标题是: " + driver.getTitle()); } }
结果还是报错,然后我又换了好几个版本的geckodriver.exe,都没有用。到现在都没有解决,希望以后能找到原因。
4.最后我感觉我还是换个浏览器吧,于是换上了ie浏览器
package PRMselenium.pr; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { WebDriver driver=new InternetExplorerDriver(); driver.get("http://www.baidu.com/"); System.out.println("第一个页面的标题是: " + driver.getTitle()); } }
结果报错了
我去报错提示的地址下载了一个和我系统匹配的64位的IEDriverServer.exe,http://selenium-release.storage.googleapis.com/index.html
5.修改之后的代码
package PRMselenium.pr; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { System.setProperty("webdriver.ie.driver","C:\Program Files (x86)\Internet Explorer\IEDriverServer.exe"); WebDriver driver=new InternetExplorerDriver(); driver.get("http://www.baidu.com/"); System.out.println("第一个页面的标题是: " + driver.getTitle()); } }
运行之后,终于跑起来了(经历了一周的时间才跑起来第一个脚本)。
在之后我发现64位的IEDriverServer会导致在win10上运行输入数字或者字母之类字符串的时候速度非常慢,经过百度查了下原因,换上了一个32位的IEDriverServer。