大家都知道,selenium2對火狐瀏覽器兼容性比較好,和谷歌和IE相比,好處是無需安裝相應的driver.exe來支持啟動瀏覽器,但是缺點是最高支持火狐47版本。
現在selenium3出來了,是不是支持高版本的火狐瀏覽器了呢,答案是肯定的而且火狐瀏覽器必須是48或者更高版本,還需要geckodriver來支持。把geckodriver放在path路徑下即可。
代碼如下:
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")
個別電腦使用以上代碼啟動可能報以下錯誤
解決方法:指定瀏覽器的路徑,具體要看自己瀏覽器的安裝路徑
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鼠標操作類執行雙擊、移動鼠標到元素等事件都報錯:
Message: POST /session/7e857f95-4522-4898-abce-384e4ec00ca2/actions did not match a known command
原因這是Mozilla/geckodriver的一個bug,由於geckodriver開發是2016年中旬的,所以只能這對firefox47或者更老的版本使用,如果你要使用selenuym3+firefox,請使用較老版本的firefox。經測試selenium3+Chrome58使用正常,及時現在geckodriver有更新版本,解決了以上的鼠標操作事件問題,還是出現了其他問題,如設置瀏覽器最大化等等。