C# 硒铬驱动在短时间内逐渐变慢

C# 硒铬驱动在短时间内逐渐变慢,c#,selenium,selenium-chromedriver,C#,Selenium,Selenium Chromedriver,我使用Selenium和chromedriver来做一些非常基本的屏幕抓取,但当我循环等待元素,然后点击链接时,这个过程变得非常缓慢,无法使用。如果我处理了chromedriver实例并新建了另一个实例,那么一切又会很快开始 为什么速度变得如此之快?我可以做些什么来加快速度? 伪C#代码: while(true) { var dataGridRows=browser.Driver.FindElements(By.XPath(“./*[@class='datadrid-row']); foreac

我使用Selenium和chromedriver来做一些非常基本的屏幕抓取,但当我循环等待元素,然后点击链接时,这个过程变得非常缓慢,无法使用。如果我处理了chromedriver实例并新建了另一个实例,那么一切又会很快开始

为什么速度变得如此之快?我可以做些什么来加快速度?

伪C#代码:

while(true)
{
var dataGridRows=browser.Driver.FindElements(By.XPath(“./*[@class='datadrid-row']);
foreach(dataGridRows中的var dataGridRow)
{
OpenQA.Selenium.Interactions.Actions act=新的OpenQA.Selenium.Interactions.Actions(browser.Driver);
//双击网格行。将显示一个新对话框(CSS样式)。
双击(dataGridRow.Build().Perform();
//这是我第一次双击网格行,对话框在几毫秒内弹出。
//这会逐渐变慢,大约10次迭代后,双击后弹出窗口将需要几分钟时间显示。
//这一行等待“关闭对话框”按钮出现。这是延迟发生的地方,但一旦对话框实际出现,它就会返回。
ReadOnlyCollection closeButtons=browser.Driver.FindElements(By.XPath(“./*[@class='dilog-close-button']”);
收集信息(…);
}
睡眠(1000*60);
}

驱动程序本身并没有消耗您的内存,而是浏览器。是的,创建新的驱动程序实例可以加快速度,但不是新的
chromedriver.exe
实例加快了速度,而是新的
chrome.exe
实例。如果您手动执行有问题的脚本(尝试单击并滥用页面10分钟,看看会发生什么),您会发现同样的问题,浏览器会变慢。 但我必须承认,根据我的经验,chrome是速度最快、性能最好的驱动程序,
firefoxdriver
在同一个实例被大量使用时会出现更多内存问题,
IEdriver
在长时间只有一个实例时是不可能使用的()。即使是
phantomJS/ghostdriver
也有这样的问题


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

关闭按钮不等待任何操作。它只是在页面上搜索XPath对话框关闭按钮。我的猜测是,您正在某个地方设置隐式等待,这导致了问题。另外,您是否混合了隐式和显式等待?医生说不要这样做,因为这会导致不可预测的等待时间。你是否手动尝试过这种情况?也许是网站。您是否尝试过FF或其他浏览器驱动程序?它有不同的表现吗?
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);
}