硒2.0 IE的Xpath性能 [英] Selenium 2.0 IE Xpath Performance

查看:183
本文介绍了硒2.0 IE的Xpath性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用硒的dotnet-2.0a5遍历许多表格,并有使用XPath。例如;

I'm attempting to use selenium-dotnet-2.0a5 to iterate through many tables, and have to use xpath. e.g;

var tableRows = _table.FindElements(By.TagName("tr"));

foreach (var row in tableRows)
{ 
    row.FindElements(By.XPath("td|th"));
    //iterate through tablecells and get text of each
}

平均次数遍历约50行,火狐0-2秒,铬6-8秒,IE 60-70秒。

Average times to iterate through about 50 rows, firefox 0-2 sec, chrome 6-8 sec, IE 60-70 sec.

我的大多数测试需要在IE中运行,在我能做些什么,以获得更好的性能XPath的任何提示?

Most of my tests need to be run in IE, any tips on what can I do to get better xpath performance?

推荐答案

如果你有机会改变HTML,可以尝试在类的声明上表中的数据元素。然后,你可以使用By.ClassName而不是XPath的。

If you have access to change the HTML, try putting in a class declaration on the table data elements. Then you could use By.ClassName instead of XPath.

但在此之前我去任何进一步的,究竟是你想怎么办?这似乎很奇怪,

But before I go any further, what exactly are you trying to do? It seems odd that

在CssSelectors在.Net和IE是完全supprted这将是一个很好的选择,但现在它是不可靠的。请记住,现在,你的文档需要在标准模式来呈现。

Once CssSelectors is fully supprted in .Net and IE it'll be a great option, but for now it's not reliable. Remember for now, your document needs to be rendered in Standards mode.

您将要考虑看着眼前TD和TD没有和日。虽然它肯定是可行的,它增加了一定的复杂性。我做了下面的纯朴的缘故。通常情况下,你会知道有多少次也有和他们持有什么,并分别处理它们。

You'll want to consider looking at just td and not td and th. While it's certainly doable, it adds a certain amount of complexity. I've done that below for simplicities sake. Typically you'd know how many th there are and what they hold, and deal with them separately.

获取到code,我发现有轻微加速去By.TagName。这花了大约20秒超过43行×4列。

Getting onto the code I found there was a slight speedup going to By.TagName. This took about 20 seconds over 43 rows by 4 columns.

        IWebElement table = driver.FindElement(By.TagName("table"));
        ReadOnlyCollection<IWebElement> cells = table.FindElements(By.TagName("td"));
        foreach (IWebElement cell in cells)
        {
            Console.WriteLine(cell.Text);
        }

但后来我尝试加载网页的源文件到内存中,并使用 HtmlAgilityPack 解析页面。警惕使用XML解析器读取html格式的文档,你会发现HTML可能不是完美的XML。下面code了,几乎猥亵96的毫秒

But then I tried loading the page source into memory and parsing the page using the HtmlAgilityPack. Be wary of using XML parsers to read html docs, you'll find html may not be perfect XML. The following code took and almost obscene 96 milliseconds

        HtmlDocument html = new HtmlDocument();
        html.LoadHtml(driver.PageSource);
        HtmlNodeCollection nodeCollect =  html.DocumentNode.SelectNodes("//td");
        foreach (HtmlNode node in nodeCollect)
        {
            Console.WriteLine(node.InnerText);
        }

一起去加载页面的源代码和解析,如果你想这样做,通过文档检查元素循环。还原到你的驱动程序时,您需要导航/交互。

Go with loading page source and parsing, if all you want to do it iterate through a document checking elements. Revert back to your driver when you need to navigate/interact.

这篇关于硒2.0 IE的Xpath性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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