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

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

Mysql sorting and paging (order by & limit) and existing pitfalls

Sorting query (order by) In e-commerce: We want t...

How to install Oracle on Windows Server 2016

1. Install Oracle There are too many Oracle insta...

Quickly solve the problem of slow and stuck opening of input[type=file]

Why is it that when the input tag type is file an...

Summary of web designers' experience and skills in learning web design

As the company's influence grows and its prod...

How to use an image button as a reset form button

When we make a form, we often set a submit button ...

CentOS 8 installation diagram (super detailed tutorial)

CentOS 8 is officially released! CentOS fully com...

Go to another file after submitting the form

<br />Question: How to write in HTML to jump...

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Example code for making the pre tag automatically wrap

The pre element defines preformatted text. Text en...

JavaScript to achieve simple provincial and municipal linkage

This article shares the specific code for JavaScr...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

A brief discussion on the implementation principle of Vue slot

Table of contents 1. Sample code 2. See the essen...