在ie上使用xpath时selenium运行缓慢的解决办法

来源:互联网 发布:知乎客户端无法登陆 编辑:程序博客网 时间:2024/06/05 17:35
 在ie上运行selenium的测试程序时,如果使用的是xpath,会发现速度奇慢无比,
例如这样一个测试

Java代码 复制代码 收藏代码
  1. assertTrue(selenium.isElementPresent("//div[@id='content']/div[1]/table/tbody/tr[2]/td[2]"));  


在firefox上的时间是毫秒级的,但在ie上却要数十秒,经过查找资料,终于找到
了解决办法:

1. 更换默认的xpath库
  除了ie,其他主要浏览器都是内置对xpath的支持的,但ie不行,所以selenium
  使用了javascript库,默认使用的是ajaxslt,这个会比较慢,可以换成
  javascript-xpath,  虽然比firefox还是慢,但也快多了,上面的例子只需要不
  到1秒。换法很简单,如下:
 
Java代码 复制代码 收藏代码
  1. selenium = new DefaultSelenium(location, port, browser, targetPath);   
  2. selenium.start();   
  3. selenium.useXpathLibrary("javascript-xpath");  


2. 写xpath时,尽量从一个具有id的元素开始,这样也可以大大提高执行速度,例如
  如果上面的测试写成下面这样,运行时间就会变成几秒了。
 
Java代码 复制代码 收藏代码
  1. assertTrue(selenium.isElementPresent("//div[1]/table/tbody/tr[2]/td[2]"));  
原创粉丝点击