为什么current_url会在Ubuntu 16.04上提升“Selenium&PhantomJS无法解码JSON对象”?
问题描述:
这里的摄制:为什么current_url会在Ubuntu 16.04上提升“Selenium&PhantomJS无法解码JSON对象”?
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
注:
- 我在虚拟机上运行安装所有更新(
sudo apt update
和sudo apt dist-upgrade
已经运行)。 - 此问题似乎也被捕获为Ubuntu bug 1569899。我还没有尝试修复建议(从tarball构建PhantomJS)。
- 的
QT_QPA_PLATFORM=offscreen
变量是修复错误: “QXcbConnection:无法连接到显示”(Debian错误817277,PhantomJS问题14376)
UPDATE我添加了一个简单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
。