簡體   English   中英

硒python無法與phantomjs一起使用

[英]selenium python not working with phantomjs

我已經通過python將phantomjs與npm和selenium-python一起安裝了。

一切都可以在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: 

有什么解決辦法嗎?

確保您至少使用PhantomJS 1.8。直到該版本,他們才支持Webdriver連接。 我知道我的軟件包管理器昨天有一個較舊的版本。 http://phantomjs.org/release-1.8.html

PhantomJS

self.driver = webdriver.PhantomJS(“ C://phantomjs.exe”)
self.driver.get(“ http://python.org/ ”)

或者|

  1. 打開cmd提示
  2. 在cmd中找到文件夾C:\\ Python27 \\ Scripts,然后輸入brew install phantomjs

self.driver = webdriver.PhantomJS()
self.driver.get(“ http://python.org/ ”)

我建議嘗試以下方法:

driver = webdriver.PhantomJS(executable_path = r'可執行文件的路徑')

說明可執行文件的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM