Chrome webdriver下载-避坑

06-01 1438阅读

WebDriver以原生的方式驱动浏览器,不需要调整环境变量。

一、window版

1.chrome和chromedriver下载地址:

Chrome for Testing availabilityChrome webdriver下载-避坑

我下载的是如下两个安装包,解压即可。

2.导包

pip install selenium

然后用python代码引用即可

二、Linux版

1.chrome和chromedriver下载地址:Chrome for Testing availability

下载两个包,参考上图wins版

操作步骤:

#chrome
unzip chrome-linux64.zip
sudo mv chrome-linux64 /opt/google-chrome
sudo ln -s /opt/google-chrome/chrome /usr/bin/google-chrome
#通过在终端中输入 google-chrome 来运行 Chrome
#chromdriver
unzip chromedriver-linux64.zip
sudo mv chromedriver-linux64/chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
#使用方法
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
def get_webdriver():
    options = Options()
    options.add_argument("--headless")  # 如果需要无头模式
    service = Service('/usr/local/bin/chromedriver')
    return webdriver.Chrome(service=service, options=options)
# 使用 WebDriver
driver = get_webdriver()
driver.get('https://www.example.com')
print(driver.title)
driver.quit()

查看版本看是否匹配:

chromedriver --version
google-chrome --version

报错:

google-chrome: error while loading shared libraries: libatk-1.0.so.0: cannot open shared object file: No such file or directory

解决:表明系统缺少一些 Chrome 运行所需的基础库。在 Linux 系统上,需要安装一些额外的依赖包。

sudo apt-get update
sudo apt-get install -y \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    libnspr4 \
    libnss3

如果出现渲染出的图片乱码:安装中文包

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y fonts-wqy-zenhei fonts-wqy-microhei
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码