你們都知道,selenium2對火狐瀏覽器兼容性比較好,和谷歌和IE相比,好處是無需安裝相應的driver.exe來支持啓動瀏覽器,可是缺點是最高支持火狐47版本。web
如今selenium3出來了,是否是支持高版本的火狐瀏覽器了呢,答案是確定的並且火狐瀏覽器必須是48或者更高版本,還須要geckodriver來支持。把geckodriver放在path路徑下便可。瀏覽器
代碼以下:session
1 #coding=utf-8 2 from selenium import webdriver 3 from time import sleep 4 from selenium.webdriver.common.action_chains import ActionChains 5 browser = webdriver.Firefox() 6 browser.get("https://www.baidu.com")
個別電腦使用以上代碼啓動可能報如下錯誤測試
解決方法:指定瀏覽器的路徑,具體要看本身瀏覽器的安裝路徑spa
1 #coding=utf-8 2 from selenium import webdriver 3 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 4 binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') 5 browser = webdriver.Firefox(firefox_binary=binary) 6 browser.get("https://www.baidu.com")
可是導入ActionChains鼠標操做類執行雙擊、移動鼠標到元素等事件都報錯:firefox
Message: POST /session/7e857f95-4522-4898-abce-384e4ec00ca2/actions did not match a known command3d
緣由這是Mozilla/geckodriver的一個bug,因爲geckodriver開發是2016年中旬的,因此只能這對firefox47或者更老的版本使用,若是你要使用selenuym3+firefox,請使用較老版本的firefox。經測試selenium3+Chrome58使用正常,及時如今geckodriver有更新版本,解決了以上的鼠標操做事件問題,仍是出現了其餘問題,如設置瀏覽器最大化等等。code