无法通过Selenium在python中运行PhantomJS [英] Can't run PhantomJS in python via Selenium
问题描述
过去3天,我一直在尝试通过硒运行PhantomJS,但没有成功. 到目前为止,我已经尝试通过npm安装PhantomJS,从源代码构建PhantomJS,通过apt-get安装并下载预构建的可执行文件并将其放置在/usr/bin/phantomjs中.
I have been trying to run PhantomJS via selenium for past 3 days and have had no success. So far i have tried installing PhantomJS via npm, building it from source, installing via apt-get and downloading prebuilt executable and placing it in /usr/bin/phantomjs.
每次我都能运行此示例脚本loadspeed.js:
Every time I was able to run this example script loadspeed.js :
var page = require('webpage').create(),
system = require('system'),
t, address;
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
}
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
并使用"phantomjs test.js http://google.com "运行它,并且按预期方式运行.
and run it with 'phantomjs test.js http://google.com' and it worked just as it should.
但是在这个小的python脚本中通过硒运行PhantomJS会产生错误:
but running PhantomJS via selenium in this small python script produces errors:
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get('http://seleniumhq.org')
python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
browser.get('http://seleniumhq.org/')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 176, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 162, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 350, in execute
return self._request(url, method=command_info[0], data=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 382, in _request
resp = self._conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1045, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 409, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 373, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''
用浏览器= webdriver替换第二个LOC.Firefox()正常工作.
Replacing second LOC with browser = webdriver.Firefox() works fine.
我在Ubuntu 13.10桌面上,并且在Ubuntu 13.04上也会发生相同的错误.
I am on Ubuntu 13.10 desktop and same error occurs on Ubuntu 13.04 aswell.
Python:2.7 PhantomJS:1.9.2
Python: 2.7 PhantomJS: 1.9.2
我在做什么错了?
推荐答案
较新的Selenium中似乎引入了一些问题,请参见
There seems to be some issue introduced in newer Selenium, see
http://code.google.com/p/selenium/issues/detail?id = 6690
我通过使用进一步使用了
I got a bit further using by using
pip install selenium==2.37
避免上面的堆栈跟踪.仍然存在driver.save_screenshot('foo.png')问题,导致文件为空.
Avoids the stack trace above. Still having problems with driver.save_screenshot('foo.png') resulting in an empty file though.
这篇关于无法通过Selenium在python中运行PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!