webdriver 不同浏览器驱动

 第一次使用ChromeDriver时,系统会报异常, Java.lang.IllegalStateException

提示The path to the chromedriver executable must be set by the webdriver.chrome.driver system property;

 

      原因是系统没有安装chromedriver,需要从http://code.google.com/p/chromedriver/downloads/list进行下载;

下载后,将文件解压放在某一目录下,如C:\Program Files\Chrome WebDriver\chromedriver.exe,

      在java程序中添加

      System.setProperty("webdriver.chrome.driver","C:/Program Files/Chrome WebDriver/chromedriver.exe");
      WebDriver wd=new ChromeDriver();

这样,ChromeDriver就可以顺利打开了。



如果使用firefox浏览器没有默认安装也要设置浏览器默认路径

  1. System.setProperty("webdriver.firefox.bin""C:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  2. WebDriver webDriver = new FirefoxDriver();  


原文在这里:http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

问题一:如何用WebDriver执行JavaScript

[ruby]  view plain  copy
  1. WebDriver driver; <span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>//</strong></span> Assigned elsewhere  
  2. JavascriptExecutor js = <span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>(</strong></span>JavascriptExecutor<span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>)</strong></span> driver;  
  3. js.<span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,0,204)">executeScript</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>(</strong></span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,102,0)">"return document.title"</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>)</strong></span>;  

问题二:为什么我执行JavaScript后一直返回nil?

You need to return from your javascript snippet to return a value, so:

[ruby]  view plain  copy
  1. js.<span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,0,204)">executeScript</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>(</strong></span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,102,0)">"document.title"</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>)</strong></span>;  

will return null, but:

[ruby]  view plain  copy
  1. js.<span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,0,204)">executeScript</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>(</strong></span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(153,102,0)">"return document.title"</span><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,102,0)"><strong>)</strong></span>;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值