selenium + python automation: selenium start to build chrome browser on centos headless no interface mode

Reprinted: https://www.cnblogs.com/yoyoketang/p/11582012.html

 

Foreword

selenium running on windows machines, each interface starts to run very unstable. I think of the headless come with a chrome-free interface mode, does a lot of convenience.
In order to improve the efficiency and stability of automated operation, so the deployment of selenium automation environment to the linux server, which is more convenient.
Environment:
centons 7.6
Python 3.6
Chrome 77.0.3865.90
chromedriver 77.0.3865.40
the Selenium 3.14

Install the latest version of chrome

Method One: After downloading to your local installation

Google-chrome to download the latest version 77.0.3865.90 (some small partners may not download, so I put QQ group 750,815,713, group file download)

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

Local installation package after the download is complete, use yum

yum localinstall google-chrome-stable_current_x86_64.rpm

Method Two: yum install online

yum install google-chrome-stable_current_x86_64.rpm

Or specified address

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

After the installation is complete, check the version number under

> google-chrome -version

Google Chrome 77.0.3865.90 

chromedriver drive

Download chromedriver driver, version history http://npm.taobao.org/mirrors/chromedriver find the corresponding driver version

You can use wget to download the zip package

wget http://npm.taobao.org/mirrors/chromedriver/77.0.3865.40/chromedriver_linux64.zip

Extract the zip package, if prompted no zip, it is yum -y install zipfirst mounted

Extract the zip unzip chromedriver_linux64.zip #

Unzip chromedriver move to the next / usr / bin / directory

mv chromedriver /usr/bin/

View chromedriver version number

> chromedriver --version

ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442})

Installation selenium

Install the latest version of selenium 3.141.0

pip3 install selenium

[root@yoyo chrome]# pip show selenium
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/ Author: UNKNOWN Author-email: UNKNOWN License: Apache 2.0 Location: /usr/local/python3/lib/python3.6/site-packages Requires: urllib3 Required-by: [root@yoyo chrome]# 

Run selenium Code

Test_demo.py create a new file, run the test code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless') # 无界面 chrome_options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在报错问题 chrome_options.add_argument('--disable-gpu') # 禁用GPU硬件加速。如果软件渲染器没有就位,则GPU进程将不会启动。 chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--window-size=1920,1080') # 设置当前窗口的宽度和高度 driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options) #driver = webdriver.Chrome() driver.get("https://www.cnblogs.com/yoyoketang/") print(driver.page_source) driver.quit()

Run the code

python3 test_demo.py

Page appears - the contents of the "Shanghai yo" blog home page, run the above success

Copyright © 2019 上海-悠悠
<br><span id="poweredby">Powered by .NET Core 3.0.0-preview9-19423-09 on Linux</span> </div><!--end: footer --> </div><!--end: home 自定义的最大容器 --> </body></html> 

Runs on linux selenium efficiency will improve a lot yo!

Guess you like

Origin www.cnblogs.com/xiaomifeng0510/p/12072081.html