[英]selenium python not working with phantomjs
I have installed phantomjs with npm and selenium-python via python. 我已经通过python将phantomjs与npm和selenium-python一起安装了。
Everything works properly with Headless Firefox, but not with phantomjs. 一切都可以在Headless Firefox上正常运行,但不能与phantomjs一起运行。
Here is the code. 这是代码。
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:
Any solution to this ? 有什么解决办法吗?
Make sure you're at least at PhantomJS 1.8 They didn't support webdriver connections until that version. 确保您至少使用PhantomJS 1.8。直到该版本,他们才支持Webdriver连接。 I know my package manager had a much older version when I was going through this yesterday. 我知道我的软件包管理器昨天有一个较旧的版本。 http://phantomjs.org/release-1.8.html http://phantomjs.org/release-1.8.html
self.driver = webdriver.PhantomJS("C://phantomjs.exe") self.driver = webdriver.PhantomJS(“ C://phantomjs.exe”)
self.driver.get(" http://python.org/ ") self.driver.get(“ http://python.org/ ”)
or| 或者|
brew install phantomjs
在cmd中找到文件夹C:\\ Python27 \\ Scripts,然后输入brew install phantomjs
self.driver = webdriver.PhantomJS() self.driver = webdriver.PhantomJS()
self.driver.get(" http://python.org/ ") self.driver.get(“ http://python.org/ ”)
I would suggest trying the following: 我建议尝试以下方法:
driver = webdriver.PhantomJS(executable_path=r'path to executable') driver = webdriver.PhantomJS(executable_path = r'可执行文件的路径')
where the path the the executable is stated. 说明可执行文件的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.