Configure selenium environment based on linux and implement operation

Configure selenium environment based on linux and implement operation

1. Using Selenium in Linux

1. Install Chrome

Install Google Chrome using the following command

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

You can also download it locally and then install it

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

Install Necessary Libraries

yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts

2. Install chromedriver (the corresponding versions of chrome and chromedriver are attached at the end)

chrome official website

wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip

Taobao source (recommended)

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

Unzip the downloaded file and place it in the following location:

unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/

Grant execute permissions

chmod +x /usr/bin/chromedriver

3. Run the code to see if it is successful (under Python)

from selenium import webdriver
driver = webdriver.Chrome()

------------2019 compatible version comparison table-----------
ChromeDriver 78.0.3904.11 (2019-09-12)---------Supports Chrome version 78
ChromeDriver 77.0.3865.40 (2019-08-20)---------Supports Chrome version 77
ChromeDriver 76.0.3809.12 (2019-06-07)---------Supports Chrome version 76
ChromeDriver 75.0.3770.8 (2019-04-29)---------Supports Chrome version 75
ChromeDriver v74.0.3729.6 (2019-03-14)--------Supports Chrome v74
ChromeDriver v2.46 (2019-02-01)----------Supports Chrome v71-73

2. Chrome runs in non-interface mode

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')#Solve the error that the DevToolsActivePort file does not existchrome_options.add_argument('window-size=1920x3000') #Specify the browser resolutionchrome_options.add_argument('--disable-gpu') #Google documentation mentions that this attribute needs to be added to avoid bugs
chrome_options.add_argument('--hide-scrollbars') #Hide scrollbars, for some special pageschrome_options.add_argument('blink-settings=imagesEnabled=false') #Do not load images, increase speedchrome_options.add_argument('--headless') #The browser does not provide visualization pages. If the system does not support visualization under Linux, the startup will fail without this addition#Create browser objectdriver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)#executable_path: browser driver pathdriver.get(url)

3. Download files in non-interface mode

Previously, Chromedriver running in headless mode would not correctly download files due to it sparsely parsing the preferences file provided to it. An engineer from the headless Chrome team suggested using DevTools' "Page.setDownloadBehavior" to fix this issue. This changelist implements this fix. Downloaded files default to the current directory, which can be set using download_dir when instantiating a chromedriver instance. Also added tests to ensure correct download functionality.

params = {'behavior': 'allow', 'downloadPath': r'C:\Users\Debanjan.B\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of using Selenium Chrome under Linux
  • Analysis of Linux configuration to achieve key-free login process
  • Detailed tutorial on uploading and configuring jdk and tomcat on linux
  • Installation and configuration method of Zabbix Agent on Linux platform
  • vscode Linux C++ development code automatic prompt configuration under win10 environment (based on WSL)
  • Linux process management tool supervisor installation and configuration tutorial
  • Summary of Linux environment variable configuration methods (differences between .bash_profile and .bashrc)

<<:  Native js to implement drop-down menu

>>:  Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Recommend

MySQL 5.7.18 release installation guide (including bin file version)

The installation process is basically the same as...

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

Detailed explanation of docker visualization graphics tool portainer

Table of contents 1. Introduction to Portainer 2....

JavaScript to implement a simple shopping form

This article shares the specific code of JavaScri...

Implementation of 2D and 3D transformation in CSS3

CSS3 implements 2D plane transformation and visua...

Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Table of contents 1. Installation preparation 1. ...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

In-depth study of MySQL composite index

A composite index (also called a joint index) is ...

Commonly used English fonts for web page creation

Arial Arial is a sans-serif TrueType font distribu...

MySQL cleverly uses sum, case and when to optimize statistical queries

I was recently working on a project at the compan...

Detailed explanation of how to use Vue to load weather components

This article shares with you how to use Vue to lo...

Several skills you must know when making web pages

1. z-index is invalid in IE6. In CSS, the z-index...