1. 程式人生 > >Linux下使用selenium時出現'geckodriver' executable needs to be in PATH.錯誤,解決方法

Linux下使用selenium時出現'geckodriver' executable needs to be in PATH.錯誤,解決方法

今天想用selenium自動開啟12306網址

程式碼如下:

    def __init__(self):  #建構函式   初始化
        self.initmy_url = 'https://kyfw.12306.cn/otn/view/index.html'#登陸成功後的網址
        self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'#定義一個登陸網址屬性
        self.driver = webdriver.Firefox() # 建立一個Firefox WebDriver的例項 要寫上驅動所在路徑
    #定義一個函式實現登陸
    def _login(self):#_下劃線表示不想被外界呼叫
        self.driver.get(self.login_url)#driver.get 方法將開啟URL中填寫的地址

發現報如下錯誤:

 os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

解決方法如下:

解決方法為將geckodriver放入/usr/local/bin目錄下(ps:/usr/local/bin目錄是存放可執行檔案的)

開啟終端輸入以下命令

su root

cp -rf 檔案所在路徑 /usr/local/bin/  例如:cp -rf 下載/geckodriver /usr/local/bin/

完美解決!