gpt4 book ai didi

c# - Selenium ChromeDriver 在短时间内逐渐变慢

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:07 26 4
gpt4 key购买 nike

我将 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);
}

最佳答案

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

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

关于c# - Selenium ChromeDriver 在短时间内逐渐变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42351320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com