【问题标题】:Python - Selenium PhantomJS - JSON ErrorPython - Selenium PhantomJS - JSON 错误
【发布时间】:2015-09-01 21:43:21
【问题描述】:

我有一个使用 Selenium 和 PhantomJS 的小型测试 python 脚本。 Python 版本是 2.7,PhantomJS 是 1.9.2。稍后我计划将其与 BeautifulSoup 一起用于访问金融网站。

我的 python 脚本是这样的 -

from selenium import webdriver
phantomJSPath = "C:\my working dir\\Lib\phantomjs.exe"
browser = webdriver.PhantomJS(executable_path=phantomJSPath)

执行后我得到以下错误 -

文件“C:\my working dir\Test.py”,第 32 行,运行中

browser = webdriver.PhantomJS(executable_path=phantomJSPath)
File "C:\Python27\lib\site-   packages\selenium\webdriver\phantomjs\webdriver.py", line 56, in __init__
desired_capabilities=desired_capabilities)
File "C:\Python27\lib\site-  packages\selenium\webdriver\remote\webdriver.py", line 87, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 136, in   start_session
'desiredCapabilities': desired_capabilities,
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 196, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 102, in check_response
value = json.loads(value_json)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

有人知道我为什么会收到这个错误吗?

【问题讨论】:

  • PhantomJS 1.9.2 真的很旧,可能存在与 GhostDriver 和 selenium 库不兼容的问题。尝试更新到更新的 PhantomJS 版本,例如 2.0.0、1.9.8 或 1.9.7。
  • 您使用的是哪个版本的 Selenium?

标签: python selenium phantomjs


【解决方案1】:

很抱歉回答我自己的赏金问题,但对于那些可能遇到类似问题的人:http_proxy 环境变量不能很好地与 phantomjs 和 selenium 配合使用。我删除了它,一切正常。

【讨论】:

    【解决方案2】:

    在 Ubunto 15 上运行 selenium 1.9.8 调用 browser.close() 时,我遇到了类似的问题(瞬态):

      File "propertunity/soup/Soup.py", line 121, in get...
        browser.close()
      File "/home/<user>/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 473, in close
        self.execute(Command.CLOSE)
      File "/home/<user>/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
        self.error_handler.check_response(response)
      File "/home/<user>/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 102, in check_response
        value = json.loads(value_json)
      File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    

    似乎有一个已知的issue,在您完成浏览器后,close() 并没有真正清理。尝试将 close() 更改为退出并将浏览器对象设置为无。

    【讨论】:

    • 关于上述赏金,这似乎不是同一个问题(确切地说)。
    【解决方案3】:

    你应该提供额外的参数来定义browser,它会尝试分配desired_capabilities=desired_capabilities,它需要一个json对象。你可以这样做:

    from selenium import webdriver
    phantomJSPath = "C:\my working dir\\Lib\phantomjs.exe"
    desiredCap = {'platform': 'ANY', 'browserName': 'phantomjs', 'version': '', 'javascriptEnabled': True}
    browser = webdriver.PhantomJS(executable_path=phantomJSPath, port=0, desired_capabilities=desiredCap)
    

    【讨论】:

    • 我认为desired_capabilities 已经有一个默认值,这是一个字典。
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多