Selenium ChromeDriver在短时间内逐渐变慢

时间:2017-02-20 17:55:38

标签: c# selenium selenium-chromedriver

我使用Selenium和chromedriver进行一些非常基本的屏幕抓取,但是当我循环并等待元素然后点击链接时 - 这个过程变得太慢而无法使用。如果我处理了chromedriver实例并且新建了另一个实例,那么一切都会快速启动。

为什么这么快变慢?我该怎么做才能加快速度呢?

伪C#代码:

while(true)
{
    var dataGridRows = browser.Driver.FindElements(By.XPath(".//*[@class='datadrid-row']"));

    foreach (var dataGridRow in dataGridRows)
    {
        OpenQA.Selenium.Interactions.Actions act = new OpenQA.Selenium.Interactions.Actions(browser.Driver);

        //Double click on the grid row. A new dialog (CSS style) will show up.
        act.DoubleClick(dataGridRow).Build().Perform();

        // This first time I double click the grid row, the dialog pops up within a few miliseconds.
        //  This gets incrementally slower, about 10 iterations later the popup will take several MINUTES to show up after the double-click.

        //This line waits for a "close dialog" button to appear. This is where the delay occurs but this returns as soon as the dialog actually appears.
        ReadOnlyCollection<IWebElement> closeButtons = browser.Driver.FindElements(By.XPath(".//*[@class='dilog-close-button']"));

        CollectScreenScrapeInformation(...);
    }

    Sleep(1000 * 60);
}

1 个答案:

答案 0 :(得分:1)

驱动程序本身没有吃掉你的记忆,它是浏览器。是的,创建新的驱动程序实例可以加快速度,但不是新的chromedriver.exe实例加速了它,它是新的chrome.exe实例。如果您手动执行有问题的脚本(尝试点击并滥用您的页面10分钟,看看会发生什么),您会发现相同的问题,浏览器会变慢。 但我必须承认,根据我的经验,chrome是最快且性能最佳的驱动程序,firefoxdriver在使用相同实例时会有更多内存问题,并且当您只有一个实例时IEdriver无法使用实例更长的时间(Multiple tests on the same IEdriver instance - memory issues)。即使phantomJS/ghostdriver也有这种问题。

tldr;如果驱动程序实例随着时间的推移变慢,请重新创建它。