我已经通过python安装了npm和selenium-python的phantomjs。
一切都适用于Headless Firefox,但不适用于phantomjs。
这是代码。
In [1]: from selenium import webdriver
In [2]: browser = webdriver.PhantomJS()
In [3]: browser.get('http://www.google.com/')
---------------------------------------------------------------------------
CannotSendRequest Traceback (most recent call last)
<ipython-input-5-a95426e05380> in <module>()
----> 1 browser.get('http://www.google.com/')
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in get(self, url)
174 Loads a web page in the current browser session.
175 """
--> 176 self.execute(Command.GET, {'url': url})
177
178 @property
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
160
161 params = self._wrap_value(params)
--> 162 response = self.command_executor.execute(driver_command, params)
163 if response:
164 self.error_handler.check_response(response)
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.pyc in execute(self, command, params)
353 path = string.Template(command_info[1]).substitute(params)
354 url = '%s%s' % (self._url, path)
--> 355 return self._request(url, method=command_info[0], data=data)
356
357 def _request(self, url, data=None, method=None):
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.pyc in _request(self, url, data, method)
381 headers["Authorization"] = "Basic %s" % auth
382
--> 383 self._conn.request(method, parsed_url.path, data, headers)
384 resp = self._conn.getresponse()
385 statuscode = resp.status
/usr/lib/python2.7/httplib.pyc in request(self, method, url, body, headers)
956 def request(self, method, url, body=None, headers={}):
957 """Send a complete request to the server."""
--> 958 self._send_request(method, url, body, headers)
959
960 def _set_content_length(self, body):
/usr/lib/python2.7/httplib.pyc in _send_request(self, method, url, body, headers)
984 skips['skip_accept_encoding'] = 1
985
--> 986 self.putrequest(method, url, **skips)
987
988 if body and ('content-length' not in header_names):
/usr/lib/python2.7/httplib.pyc in putrequest(self, method, url, skip_host, skip_accept_encoding)
854 self.__state = _CS_REQ_STARTED
855 else:
--> 856 raise CannotSendRequest()
857
858 # Save the method we use, we need it later in the response phase
CannotSendRequest:
对此有任何解决方案吗?
答案 0 :(得分:1)
确保您至少在PhantomJS 1.8上。在该版本之前,他们不支持webdriver连接。我知道我的包管理器在我昨天经历这个版本时有一个更旧的版本。 http://phantomjs.org/release-1.8.html
答案 1 :(得分:0)
self.driver = webdriver.PhantomJS(“C://phantomjs.exe”)
self.driver.get( “http://python.org/”)
或|
brew install phantomjs
self.driver = webdriver.PhantomJS()
self.driver.get( “http://python.org/”)
答案 2 :(得分:0)
我建议尝试以下方法:
driver = webdriver.PhantomJS(executable_path = r'path to executable')
表示可执行文件的路径。