【问题标题】:Python: Selenium with PhantomJS empty page sourcePython:带有 PhantomJS 空页面源的 Selenium
【发布时间】:2014-06-28 04:16:32
【问题描述】:

当我想获取 URL 页面的来源时,我在 Windows7 上遇到了 Selenium 和 PhantomJS 的问题。 browser.page_source 仅返回 <html><head></head></html>。我在browser.page_source 之前睡过,但没有帮助。

这是我的代码:

from selenium import webdriver
browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe')
url = 'myurl'
browser.get(url)
print browser.page_source

在具有相同版本 PhantomJS 的 Linux 上,它可以完美运行。它也适用于 Windows Server 2003。

【问题讨论】:

  • 尝试一些调试,例如截图
  • 截图是空的,空白的图像。我认为这是 PhantomJS 的问题,我尝试使用 PyQt4 的 webkit 加载页面,但结果是一样的,只是 和 标签。这是让我头疼的 URL:homesearch.com/browse?fulltextquery=miami+fl&page=0
  • 我在导航到某些 https://url 时遇到了同样的问题。调用browser.get(url) 后,使用time.sleep(few_seconds) 或webdriver 的expected conditions 等待页面上出现某些元素
  • 也尝试使用这些参数创建驱动程序实例browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])
  • 我使用了 15-20 秒的睡眠时间,结果是一样的。我会尝试使用 service_args。

标签: python selenium phantomjs


【解决方案1】:

如下增加屏幕尺寸对我有用:

driver = webdriver.PhantomJS(path2phantom, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 
driver.set_window_size(2000, 1500)上一页>

【讨论】:

    【解决方案2】:
    driverPhantom = webdriver.PhantomJS(driverLocation, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])      # initaling web driver for PhantomJs
    

    为我工作。

    【讨论】:

      【解决方案3】:

      默认情况下,phantomjs 使用 SSLv3,但是在 ssl 中出现错误后的许多站点都迁移到了 tls。这就是为什么你有空白页。 使用service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']

      browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
      

      【讨论】:

      • 对此表示赞成,因为仅使用忽略 ssl 错误对我不起作用,但添加此评论帮助建议的任何 ssl 协议。荣誉
      • 请注意--ignore-ssl-errors 很危险,除非您知道自己在做什么,否则不应使用它。另一个选项可能会解决大多数问题。
      • 谢谢!节省了我很多时间。
      • @AlexW 这个解决方案对我有用,但我不知道这个。您能否详细说明使用--ignore-ssl-errors 的危险或提供一些参考?
      • 我有一个相关的问题:How can we found these selenium phantomjs usages? 我想我很难在seleniumhq.org/docs 中找到这些用法
      【解决方案4】:

      使用service_args=['--ignore-ssl-errors=true'] 成功了!

      browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])
      

      【讨论】:

      • 我正在使用webdriver.Remote,因为我正在远程运行 phantomJS。如果是这种情况,你知道如何传递 service_args 吗?
      猜你喜欢
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2018-05-29
      • 2016-08-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多