Selenium - PhantomJS - DOM 中的 Findelements 遍历速度很慢

Selenium - PhantomJS - Findelements in DOM traversal is slow

出于某些原因,我正在尝试使用 Selenium/PhantomJS 递归 DOM。 它有效但速度很慢,我不知道为什么。 Findelements 似乎每次都需要大约 250ms。

我试过将隐式等待归零,但收效甚微。我也试过使用 Xpath 没有真正的改变。

这是代码,有什么建议吗?

    public static void RecurseDomFromTop()
    {
        DomRecursor( pjsDriver.FindElement( By.TagName( "*" ) ) );
    }

    public static void DomRecursor( IWebElement node )
    {
        ReadOnlyCollection<IWebElement> iwes = node.FindElements( By.TagName( "*" ) );

        foreach (IWebElement iwe in iwes)
        {
           DomRecursor( iwe );
        }

    }

您以这种方式比较两个 dom 的方法是错误的。每次您发出 Selenium 请求时,都会创建一个 HTTP 请求,该请求被发送到驱动程序,驱动程序将其发送到浏览器,然后浏览器将其发送回驱动程序,然后驱动程序返回给您语言绑定。这涉及很多开销。

相反,您应该做的是使用 driver.PageSource 并在一次调用中获得整个 HTML 响应。稍后您可以使用 HTML 解析库,它至少比您现在采用的方法快 10 倍。

查看下面使用 HtmlAgilityPack 获取 DOM 数据的文章

https://www.codeproject.com/Articles/659019/Scraping-HTML-DOM-elements-using-HtmlAgilityPack-H