Debian/Ubuntu install Chrome and Chrome Driver and use selenium to automate testing

As of now, Chrome is still the best browser, bar none. Chrome is not only a powerful tool for daily use, it is also a must in performing automated tasks through toolkits such as Chrome Driver and selenium. I believe that everyone has already understood the configuration and use of selenium on Windows, let us see how to configure and use selenium on Linux (the method is also available without a graphical interface! )

The method described in this article is only available for Debian systems.

install chrome

First download the Chrome body and use wget directly.

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

According to my own test (July 9, 2023, Qingdao Unicom Home Broadband), all Google-related links mentioned in this article do not require scientific Internet access.

Then install it:

sudo dpkg -i google-chrome-stable_current_amd64.deb

If it succeeds, congratulations, you can go directly to the next step; after testing, there is a high probability that this step will report an error indicating that there is a lack of dependencies. It doesn’t matter. Now you only need to run the following code and execute the installation command again:

sudo apt-get install -f

install chrome-driver

Execute the following code to check the Chrome version:

google-chrome -version

Then find the corresponding version of Chrome Driver in https://chromedriver.storage.googleapis.com/index.htmlCtrl + F (you can use to search). It doesn't matter if there is no exact corresponding version, just download the one that is closest to it, for example, my version number is 114.0.5735.198, so I chose 114.0.5735.90.

1688892123614

1688892280212

Download the linux64 version in the new interface (copy the link, just use wget to download), then decompress it, and copy it to a /usr/bindirectory for easy program use (if you don’t put it in this directory, you need to manually specify the Driver in the subsequent Python program Location)

1688892363032

wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo cp chromedriver /usr/bin

test

Install seleniumthe module and try to start Chrome.

Write the following test script intotest.py

usually

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def init_driver():
    options = Options()
    options.add_argument('--no-sandbox') # 亲测 Debian 必须加,Ubuntu 随意
    options.add_argument("--headless")
    options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(options=options)
    return driver

init_driver()

print('Success')

Specify the Driver path

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


def init_driver():
    options = Options()
    options.add_argument('--no-sandbox') # 亲测 Debian 必须加,Ubuntu 随意
    options.add_argument("--headless")
    options.add_argument('--disable-gpu')
    service = Service(executable_path='/root/chromedriver')
    driver = webdriver.Chrome(options=options, service=service)
    return driver

init_driver()

print('Success')

Install selenium through pip and run the script, and you can see the success prompt.

pip install selenium
python3 test.py

Guess you like

Origin blog.csdn.net/weixin_44495599/article/details/132022178