Selenium元素选择器 - 我以为xPath是最慢? [英] Selenium Element Selectors - I thought xPath was slowest?

查看:144
本文介绍了Selenium元素选择器 - 我以为xPath是最慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个公共网站进行了一些测试,看看我能否找到一些不同的Selenium CSS选择器的性能差异。我跑了一个有五个节点的集线器; mac / chrome / local,mac / safari / local,mac / ff / local,win7 / ie9 / localVM和win8 / ie10,localVM。测试都是并行运行,试图模拟我通常运行它们的方式。我很惊讶地发现xPath选择器并没有成为我预期的魔鬼。也许我的测试有一些时髦的东西?任何人都有任何洞察力?



这里是测试代码...

  int cycles = 500; 
int yVal = 0;

getPage(http://www.princeton.edu);

/ *尝试一个没有id的元素* /
startStopwatch );
for(int i = 0; i< cycles; i ++)
yVal = driver.findElementByCssSelector(a [href ='/ main / news / events /'])。getLocation ).y;
print(By CSS:+ elapsedSeconds());

startStopwatch();
for(int i = 0; i< cycles; i ++ )
yVal = driver.findElementByCssSelector(div [id ='events'] a [href ='/ main / news / events /'])。getLocation()。y;
print CSS使用id:+ elapsedSeconds());


startStopwatch();
for(int i = 0; i< cycles; i ++)
yVal = driver.findElementByXPath(// a [@ href = \'/ main / news / events /'])。getLocation()。y;
print(By xPath:+ elapsedSeconds );

/ *尝试具有id *的元素* /
//通过id
startStopwatch();
for(int i = 0; i<周期; i ++)
yVal = driver.findElementById(events)。getLocation()。y;
print(By Id:+ elapsedSeconds());

// by CSS
startStopwatch();
for(int i = 0; i< cycles; i ++)
yVal = driver.findElementByCssSelector(div [id ='events'])。getLocation()。
print(By CSS:+ elapsedSeconds());

//不必要的长xPath表达式
startStopwatch();
for(int i = 0; i< cycles; i ++)
yVal = driver.findElementByXPath(// span [text()='News at Princeton'] / ancestor :: div [1 ] / following-sibling :: div [1])。getLocation()。y;
print(By longer xPath:+ elapsedSeconds());

//稍微缩短xPath
startStopwatch();
for(int i = 0; i yVal = driver.findElementByXPath(// span [text()='Featured Events'] / ancestor :: div [1] ).getLocation()。y;
print(By short xPath:+ elapsedSeconds());

这里是结果,显示xPath保存自己,所有时间以秒为单位,500次迭代。 / p>



Safari是迄今为止最不稳定的表演者,每次测试运行的时间都奇怪地不同。



princeton.edu是一个漂亮的运行的网页,与相当容易的选择器,但似乎建议xPath不是那么糟糕。我在测试我的工作网站时发现了非常相同的东西。



对我可能会遗漏的任何想法

解决方案

人们似乎懒惰地假设Xpath很慢,应该避免。当我面试人时,我们会说他们避开Xpath,因为它是缓慢而脆弱的。速度,如这里所示,不再是一个问题,xpath只是像写的人一样脆。在正确的情况下,Xpath真棒,可以提高性能,因为它允许您在一个命令中执行可能已经采取了几个(例如,find元素,然后迭代子元素可以在一个xpath中执行)



哦,不要让我开始对那些认为对于一个元素只有一个Xpath,而是通过右键点击firebug找到的人。


I ran some tests against a public website to see if I could find differences in the performance of a few different Selenium CSS selectors. I ran one hub with five nodes; mac/chrome/local, mac/safari/local, mac/ff/local, win7/ie9/localVM, and win8/ie10,localVM. The tests were all running in parallel, to try to simulate how I usually run them. I was surprised to see that xPath selectors did not turn out to be the devil that I expected. Maybe there is something funky about my tests? Anyone have any insight?

Here is the test code...

    int cycles = 500;
int yVal = 0;

getPage("http://www.princeton.edu");

/* try an element that does not have an id*/
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("a[href='/main/news/events/']").getLocation().y;
print("By CSS: " + elapsedSeconds());

startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("div[id='events'] a[href='/main/news/events/']").getLocation().y;
print("By CSS using id: " + elapsedSeconds());


startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//a[@href=\'/main/news/events/']").getLocation().y;
print("By xPath: " + elapsedSeconds());

/* try an element with an id */
//by id
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementById("events").getLocation().y;
print("By Id: " + elapsedSeconds());

//by CSS
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("div[id='events']").getLocation().y;
print("By CSS: " + elapsedSeconds());

// an unnecessarily long xPath expression
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//span[text()='News at Princeton']/ancestor::div[1]/following-sibling::div[1]").getLocation().y;
print("By longer xPath: " + elapsedSeconds());

// somewhat shorter xPath
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//span[text()='Featured Events']/ancestor::div[1]").getLocation().y;
print("By shorter xPath: " + elapsedSeconds());

Here are results, showing xPath to hold its own, all times are in seconds for 500 iterations.

Safari was by far the most erratic performer, with times being strangely different for each test run.

princeton.edu is a pretty run-of-the-mill web page, with fairly easy selectors, but seems to suggest that xPath ain't that bad. I found very much the same thing when testing my work site.

Any thoughts on what I may be missing here??

解决方案

People seem to lazily assume Xpath is slow and should be avoided. When I interview people I cringe when they say they avoid Xpath because it is slow and brittle. The speed, as shown here, is no longer a concern, and xpath is only as brittle as the person who wrote it. In the right scenario Xpath is awesome and can actually improve performance as it allows you to perform in one command that may have taken several (e.g find element then iterate through sub elements can be performed in one xpath)

Oh, and dont get me started on people who think there is only one Xpath for an element and that it is found by right clicking in firebug

这篇关于Selenium元素选择器 - 我以为xPath是最慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆