Selenium WebDriver 工作但很慢(Java)

标签 selenium webdriver screenshot selenium-webdriver

我正在使用 Selenium WebDriver 截取网页的屏幕截图。它运行良好。但是,从我在 eclipse 中运行到屏幕截图出现在我的本地驱动器中的时间是 7-10 秒。大多数延迟似乎是启动 Firefox。

代码:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.cnn.com");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\test\\screenshot.png"));

我怎样才能加快这个过程?有没有办法可以使用已经打开的 Firefox 浏览器来节省打开新浏览器的费用?这段代码是不是很重?

详细信息:在 CentOS box 和 Win7 box 上都使用 eclipse 进行了尝试。 myspeedtest.net 显示下降 22 Mbps 和上升 1 Mbps。

最佳答案

您所要求的(允许 WebDriver 附加到正在运行的浏览器)现在已成为 almost 3 years 的问题。 .到目前为止,这个功能还没有被添加。据我所知,没有办法让 Firefox 加载得更快。不过,您可以尝试其他一些技巧。

fp.setPreference("webdriver.load.strategy", "unstable")

引用来自 this page 的警告:

There is beta feature to make firefox not wait for the full page to load after calling .get or .click. This may cause immediate find's to break, so please be sure to use an implicit or explicit wait too. This is only available for Firefox and not other browsers.



你也可以试试loading with a profile看看这是否有帮助。

关于Selenium WebDriver 工作但很慢(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13020841/

相关文章:

javascript - C# Selenium 无法定位用 Javascript 编写的隐藏元素

ruby - 如何使用 ruby​​ 和 unix 服务器截取网页的屏幕截图?

java - 使用 ChromeDriver 和 Chrome 通过 CLI 执行测试时出现 NoSuchMethodError : com. google.common.base.Preconditions.checkState(ZLjava/lang/String;)

java - 如何使用 phantomjs headless 浏览器处理 selenium 中的警报

maven - 使用 Maven 命令行以编程方式运行 TestNG

android - 在 React Native 应用程序中禁用屏幕捕获/屏幕截图

java - 保存到桌面的图像会覆盖之前的图像

java - org.testng.TestNGException : sun. 安全.provider.certpath.SunCertPathBuilderException

css - Selenium 单击具有相同类名的 CSS elmenents

python - 如何在Python中使用selenium在不同WebDriver打开的不同chrome浏览器窗口之间切换?