安裝及使用的中文文檔可以在這里找到readthedocs.io
安裝
# pip install selenium
配置
下載驅(qū)動
selenium3.x以上需要瀏覽器驅(qū)動怯疤,到這里下載相應(yīng)驅(qū)動,https://github.com/SeleniumHQ/selenium/blob/master/py/docs/source/index.rst
解壓
解壓驅(qū)動到/usr/bin
或/usr/local/bin
測試
運(yùn)行下面代碼甫题,測試是否能正常使用
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
assert 'Python' in driver.title
elem = driver.find_element_by_name('q')
elem.send_keys('pycon')
elem.send_keys(Keys.RETURN)
assert 'No results found.' not in driver.page_source
# driver.close()
問題
No such file or directory: 'geckodriver'
沒有裝上驅(qū)動巧鸭,或沒有放到指定位置
Message: can not connect to the service geckodriver
可能是沒有l(wèi)ocalhost,打開/etc/hosts
凿宾,看里面有沒有這樣一句
127.0.0.1 localhost
沒有就加上