【问题标题】:Selenium and PhantomJS takes 30 seconds to open each linkSelenium 和 PhantomJS 需要 30 秒才能打开每个链接
【发布时间】:2015-01-31 19:47:47
【问题描述】:

我正在尝试使用 Selenium 和 PhantomJS 打开一个网站并获取一些数据,但是打开一个网站需要很长时间(大约 30 秒)。每次我打开其他链接时,我都必须等待 30 多秒。我的代码有什么问题?

        static void Main(string[] args)
        {
        IWebDriver browser = new PhantomJSDriver();

        var URL = "http://www.cbssports.com/nba/playerrankings ";

        browser.Navigate().GoToUrl(URL);

        //Position
        var title = browser.FindElements(By.CssSelector(".tableTitle"));
        Console.WriteLine(title.First().Text);

        Console.Read();
        }

我尝试做的事情:
1.Set PhantomJS proxy type to none
2.Disable internet option: automatically detect settings
3.Disable IPv6 protocol

PhantomJS release notes 声称,Microsoft Windows 上的网络性能存在一些已知问题。根据发行说明,解决方案是将代理类型设置为无,但这不起作用。

【问题讨论】:

  • 您使用的是哪个 selenium 和 phantomjs 版本?谢谢。
  • 有一个技巧可以让 phantomjs 不加载所有资产(你不需要 css、图像等)我不记得它了,但如果你用谷歌搜索它,你会找到它.
  • 我使用 PhantomJS 1.9.8 和 Selenium 2.44.0

标签: c# performance selenium phantomjs screen-scraping


【解决方案1】:

您必须等待 30 秒,因为您尚未定义默认为 30 秒的超时。您应该使用这个预定义的驱动程序服务。

        var phantomJSDriverService = PhantomJSDriverService.CreateDefaultService();
        IWebDriver browser = new PhantomJSDriver(phantomJSDriverService);
        browser.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));

【讨论】:

    【解决方案2】:

    Selenium 的默认超时为 30 秒。您正在使用browser.FindElements()(复数版本),它将等待整整 30 秒再继续!

    您可以使用browser.manage().timeouts().implicitlyWait() 减少超时,也可以使用显式超时。

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2012-09-28
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多