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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

在CentOS 7.7 x86_64上安装python3的selenium 3模块实录

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在CentOS 7.7 x86_64上安装python3的selenium 3模块实录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安裝selenium3模塊
pip3 install selenium
如果上面的命令因為網絡問題,重試多次仍失敗,可以嘗試下面的命令
pip3 install selenium -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

配置selenium相關的環境
下載并安裝最新的Chrome版本
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum -y install google-chrome-stable_current_x86_64.rpm
檢查安裝的Chrome版本
which google-chrome-stable
/bin/google-chrome-stable
使用下面的命令得到Chrome的版本號
google-chrome-stable -version
Google Chrome 81.0.4044.113

使用下面的命令查看幫助

/bin/google-chrome-stable --help

下面對應的ChromeDriver版本,從下面的地址
https://sites.google.com/a/chromium.org/chromedriver/downloads
下載
wget https://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
cp -f chromedriver /usr/bin/
chmod 755 chromedriver
確保chrome和chromedriver版本一致,并且chromedriver的安裝位置,需要在python代碼中使用

運行測試demo
python3 selenium_demo.py

#!/usr/bin/env python3
#encoding: utf-8
#description: 測試程序控制并驅動Chrome 81
#note:需要安裝selenium3,chromedriver和Chrome版本兼容
#date: 2020-04-21from selenium import webdriverif __name__ == '__main__':options = webdriver.ChromeOptions()options.add_argument('--headless')options.add_argument('--disable-gpu')options.add_argument('--no-sandbox')url = 'https://www.baidu.com'browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=options)browser.get(url)print(browser.page_source)browser.quit()

這個python腳本,使用selenium去遠程獲取指定url的網頁源碼并打印出來。

關于selenium模塊中webdriver各API的使用方法,參見下面的鏈接

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

遇到的問題
1.chromedriver下載速度非常慢

解決方法: 測試發現,需要翻墻才能下載速度快,可以從我的ThinkPad T420的Chrome上翻Q下載該文件,再從虛擬機上下載ThinkPad T420的文件。

2.centos7中Chrome通過selenium截圖漢字顯示為方框
解決方法:在執行截圖任務的虛擬機上安裝中文字體,比如這里我選擇的是最愛的文泉驛微米黑字體,執行下面的命令

wget http://rpmfind.net/linux/centos/7.7.1908/os/x86_64/Packages/wqy-microhei-fonts-0.2.0-0.12.beta.el7.noarch.rpm
rpm -ivh wqy-microhei-fonts-0.2.0-0.12.beta.el7.noarch.rpm
安裝完成之后,直接可以看到字體已經安裝成功了。
fc-list :lang=zh
然后打開中文網頁就不會亂碼了。

3.采用selenium自帶截圖工具截取的Grafana看板的圖片截取不完全

解決方法:經過網上調研和測試發現,能否成功截取全屏,與網頁代碼的使用有一定關系。試過各種招兒之后,都未能圓滿解決,最后我實在沒撤了,硬著頭皮研究了Grafana頁面的代碼,發現里面有個唯一的div具有清晰的width和height,直接獲取它的值就能大致確定整個截圖的大小,從而巧妙解決了這個問題。

找到這個唯一關鍵的div,并且有尺寸大小,發現只有這個元素的height是真實的
<div class="react-grid-layout layout" style="height: 1590px;">

經過測試,發現下面的方法是可行的, 但是有時候執行不穩定
ele = browser.find_element_by_class_name("react-grid-layout.layout")
print(ele.size)

對于普通的網頁,使用常規思路就可以了。具體問題具體分析吧。下面以豆瓣首頁截取全屏為例,給出一個具有普遍意義的代碼selenium_douban.py實現如下:

#!/usr/bin/env python3
#encoding: utf-8
#description: 實現selenium+chromedriver+python實現豆瓣的全文截取,實測有效
#date: 2020-04-22from selenium import webdriver
import timeoptions = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--dns-prefetch-disable')
options.add_argument('--no-referrers')
options.add_argument('--disable-gpu')
options.add_argument('--disable-audio')
options.add_argument('--no-sandbox')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-insecure-localhost')driver = webdriver.Chrome(options=options)
driver.get('http://www.douban.com')
width = driver.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
height = driver.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
print(width, height)
driver.set_window_size(width + 100, height + 100)
time.sleep(5)
driver.save_screenshot('douban.png')
driver.close()

得到的效果圖如下:

?

?

總結

以上是生活随笔為你收集整理的在CentOS 7.7 x86_64上安装python3的selenium 3模块实录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。