• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

硒2.0 IE的Xpath性能

用户头像
it1352
帮助1

问题说明

我试图用硒的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?

正确答案

#1

如果你有机会改变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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgfcck
系列文章
更多 icon
同类精品
更多 icon
继续加载