python - 为什么 current_url 在 Ubuntu 16.04 上引发 "No JSON object could be decoded"w/Selenium & PhantomJS?

标签 python linux selenium ubuntu phantomjs

这是重现:

sudo apt install phantomjs
export QT_QPA_PLATFORM=offscreen
python
>>> import selenium.webdriver
>>> from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
>>> browser = selenium.webdriver.PhantomJS(desired_capabilities=DesiredCapabilities.PHANTOMJS)
>>> browser.get('http://example.com')
>>> browser.title
u'Example Domain'
>>> browser.current_url
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 454, in current_url
    return self.execute(Command.GET_CURRENT_URL)['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib.python2.7/dist-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 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

注意事项:

  • 我在安装了所有更新的 VM 中运行(sudo apt updatesudo apt dist-upgrade 已运行)。
  • 这个问题似乎也被捕获为 Ubuntu bug 1569899 .我还没有尝试建议的修复(从 tarball 构建 PhantomJS)。
  • QT_QPA_PLATFORM=offscreen 变量用于修复错误:“QXcbConnection:无法连接到显示器”(Debian 错误 817277,PhantomJS 问题 14376)

更新 我添加了一个简单的print 来捕获原始响应,它是这样的:

{'status': 404, 'value': u"Error: - Unable to load Atom 'execute_script' from file ':/ghostdriver/./third_party/webdriver-atoms/execute_script.js'"}

最佳答案

看起来 phantomjs 包只是损坏了(缺少组件)。那里有使用 npm 的解决方案,但由于我正在使用 Python,所以我想避免这种情况。这是我最终解决此问题的方法:

unset QT_QPA_PLATFORM
sudo apt purge phantomjs
wget -q https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mv phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/

我的其他二进制文件在 /usr/local/bin 中,但如果您将它们放在 /usr/bin 中,它也可以工作。

关于python - 为什么 current_url 在 Ubuntu 16.04 上引发 "No JSON object could be decoded"w/Selenium & PhantomJS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46795927/

相关文章:

python - 如何使用多线程

python - 将嵌套的 json 插入 cassandra

python - NLTK Sentence Tokenizer,自定义句子启动器

linux - 如何根据 Chef Recipe 中命令执行返回的值禁用Linux进程

c++ - 减少调试 session 中的堆栈大小以捕获无休止的递归

python - 在 Paramiko 中,如何确定是否通过 SSH key 或密码成功连接?

用于查找与文件权限相关的所需输出的 ​​Linux 脚本

python - 使用 Selenium Webdriver 检测是否发送了 WhatsApp 消息

javascript - 通过绑定(bind)获取Protractor中的UI-Grid列数据

java - 我如何从下拉菜单中选择元素