python+Selenium+FireFox浏览器webdriver安装 使用例子linux环境

python selenium自动化

  1. 安装selenium
pip install selenium
  1. 浏览器驱动安装
打开网页检查最新版本然后下载或修改最新的版本号下载https://selenium-python.readthedocs.io/installation.html#drivers
wget https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
tar -xvzf geckodriver-v0.26.0-linux64.tar.gz
chmod +x geckodriver
sudo mv geckodriver /usr/bin/
  1. 安装测试代码
from selenium import webdriver
import time

brower = webdriver.Firefox()
brower.get("http://www.baidu.com")

brower.find_element_by_id('kw').send_keys('selenium')
brower.find_element_by_id('su').click()

time.sleep(3)
print(brower.current_url)

brower.quit()
  1. 常见错误
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

表示geckodriver没有安装成功。
也可以指定目录,例如指定在当前路径下。

brower = webdriver.Firefox(executable_path='./geckodriver')
  1. 无头参数
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.add_argument('-headless') # 无头参数
brower = webdriver.Firefox(firefox_options=options)
brower.get("http://www.baidu.com")

brower.find_element_by_id('kw').send_keys('selenium')
brower.find_element_by_id('su').click()

time.sleep(3)
print(brower.current_url)
brower.quit()

未经允许不得转载:萌萌Apprentissage » python+Selenium+FireFox浏览器webdriver安装 使用例子linux环境

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

//下一个脚本特效