日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python谷歌浏览器驱动安装失败_阿里云centos7.2下安装chrome浏览器+webdriver+selenium及常见设置-傻瓜教程...

發(fā)布時(shí)間:2025/3/19 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python谷歌浏览器驱动安装失败_阿里云centos7.2下安装chrome浏览器+webdriver+selenium及常见设置-傻瓜教程... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Linux版本:阿里云CentOS Linux release 7.2.1511 (Core)

root用戶下

測(cè)試時(shí)間:2019-04-16

1.安裝chrome瀏覽器

1.1 創(chuàng)建yum源文件

cd /etc/yum.repos.d/

touch google-chrome.repo

1.2 輸入yum源信息

[google-chrome]

name=google-chrome

baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch

enabled=1

gpgcheck=1

gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

1.3 安裝google chrome

yum -y install google-chrome-stable --nogpgcheck

2.安裝chromedriver及selenium

yum install chromedriver#此處應(yīng)該注意chromedriver版本是否與chrome版本是否一致,如果不一致,請(qǐng)手動(dòng)下載chromedriver驅(qū)動(dòng)并替換

pip install selenium

默認(rèn)安裝路徑:chromedriver: /usr/bin/chromedriver

3.修改配置來執(zhí)行代碼,及常見錯(cuò)誤處理

3.1測(cè)試demo

#!/usr/bin/env python

# -*- coding=UTF-8 -*-

#測(cè)試代碼

import time

from selenium import webdriver

def test():

chromeOptions = webdriver.ChromeOptions()

chromeOptions.add_argument('--headless') #瀏覽器無窗口加載

chromeOptions.add_argument('--disable-gpu') #不開啟GPU加速

"""

解決報(bào)錯(cuò):

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally

(unknown error: DevToolsActivePort file doesn't exist)

"""

chromeOptions.add_argument('--disable-dev-shm-usage')

chromeOptions.add_argument('--no-sandbox')#以根用戶打身份運(yùn)行Chrome,使用-no-sandbox標(biāo)記重新運(yùn)行Chrome,禁止沙箱啟動(dòng)

#其它設(shè)置(可選):

#chromeOptions.add_argument('--hide-scrollbars') #隱藏滾動(dòng)條, 應(yīng)對(duì)一些特殊頁面

#chromeOptions.add_argument('blink-settings=imagesEnabled=false') #不加載圖片, 提升速度

#chromeOptions.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36") #偽裝其它版本瀏覽器,有時(shí)可以解決代碼在不同環(huán)境上的兼容問題,或者爬蟲cookie有效性保持一致需要設(shè)置此參數(shù)

#創(chuàng)建driver對(duì)象

#chrome_options=chromeOptions加載設(shè)置

#executable_path="/usr/bin/chromedriver"指定webdriver路徑(可選)

driver = webdriver.Chrome(chrome_options=chromeOptions,executable_path="/usr/bin/chromedriver")

try:

driver.get("http://www.baidu.com")

time.sleep(3)

print(driver.page_source)

except Exception as e:

print(e)

finally:

driver.quit()

if __name__ == '__main__':

test()

4.參考資料

總結(jié)

以上是生活随笔為你收集整理的python谷歌浏览器驱动安装失败_阿里云centos7.2下安装chrome浏览器+webdriver+selenium及常见设置-傻瓜教程...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。