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

上一篇 / 下一篇  2012-03-28 21:33:45 / 个人分类:Selenium

ie上运行selenium测试程序时,如果使用的是xpath,会发现速度奇慢无比, 

例如这样一个测试:

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秒。换法很简单,如下:

selenium = new DefaultSelenium(location, port, browser, targetPath);
selenium.start(); 
selenium.useXpathLibrary("javascript-xpath");


2.写xpath时,尽量从一个具有id的元素开始,这样也可以大大提高执行速度,例如
  如果上面的测试写成下面这样,运行时间就会变成几秒了。

assertTrue(selenium.isElementPresent("//div[1]/table/tbody/tr[2]/td[2]"));


TAG:

 

评分:0

我来说两句

Open Toolbar