簡體   English   中英

Selenium/PhantomJS 引發錯誤

[英]Selenium/PhantomJS raises error

我正在嘗試在Python 中運行PhantomJS驅動程序,但出現錯誤。 我讀過我應該將整個路徑作為參數傳遞,但它沒有幫助。

這是代碼:

from selenium import webdriver

# driver = webdriver.Chrome('D:\Python_projects\chromedriver_win32/chromedriver.exe') # this works
driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')

錯誤:

Traceback (most recent call last):
  File "path to script", line 8, in <module>
    driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 50, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\service.py", line 75, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver.
Screenshot: available via screen

你知道我做錯了什么嗎?

在原始字符串中創建路徑,添加 'r':

driver = webdriver.PhantomJS(executable_path=r'D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')

為簡單起見,將可執行文件與腳本放在同一目錄中:

driver = webdriver.PhantomJS() # now there's no need for a path

對我來說,以上都沒有解決問題; 我找到了代碼:

driver = webdriver.PhantomJS()

僅適用於root...

對我來說,這很好用,也不必為 PhantomJS 指定路徑您需要使用 pip 安裝 pip install phantomjs-binary 二進制包

pip install phantomjs-binary

它將下載一個大約 60MB 的包。 並根據運行環境包含適用於 windows、mac 和 Linux 的 PhantomJS。 之后,你可以使用它

from selenium import webdriver
from phantomjs_bin import executable_path

driver = webdriver.PhantomJS(executable_path=executable_path)

暫無
暫無

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

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