Configure and run selenium environment based on linux

  • 2021-08-21 22:08:54
  • OfStack

1. Using selenium in linux

1. Install chrome

Install Google Chrome with 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 the 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/

Give execution authority

chmod +x /usr/bin/chromedriver

3. Run the code to see if it succeeds (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 no interface mode


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')# Solve DevToolsActivePort Error report that the file does not exist 
chrome_options.add_argument('window-size=1920x3000') # Specify browser resolution 
chrome_options.add_argument('--disable-gpu') # Google Docs mentions the need to add this attribute to circumvent bug
chrome_options.add_argument('--hide-scrollbars') # Hide scroll bar ,  Response 1 Some special pages 
chrome_options.add_argument('blink-settings=imagesEnabled=false') # Do not load pictures ,  Lifting speed 
chrome_options.add_argument('--headless') # The browser does not provide visual pages . linux If the system does not support visualization, it will fail to start without this article 
 
# Create a browser object 
driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)#executable_path: Browser driver path 
driver.get(url)

3. Download files in no interface mode

Previously, Chromedriver running in headless mode could not download files correctly because it sparsely parsed the preference files provided to it. Engineers from the headless Chrome team recommend using "Page. setDownloadBehavior" from DevTools to solve this problem. This change list implements this fix. The downloaded file defaults to the current directory and can be set using download_dir when instantiating an chromedriver instance. Tests are also added to ensure the correct download function.

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


Related articles: