Hello, I’m Yue Chuang.

The installation of Selenium

Selenium is an automated testing tool that allows you to drive the browser to perform specific actions, such as clicking, pulling down, and so on. For some javascript-rendered pages, this fetching method works very well. Let’s take a look at the Selenium installation process.

1. Related links

  • Official website: www.seleniumhq.org
  • GitHub:github.com/SeleniumHQ/…
  • PyPI:pypi.org/project/sel…
  • Official documentation: selenium-python.readthedocs.io
  • 英 文文档 : selenium-python-en.readthedocs. IO

2. PIP installation

It is recommended to install PIP directly by executing the following command:

pip3 install selenium
Copy the code

3. The wheel is installed

In addition, you can also download the corresponding wheel file from PyPI to install it (download address: pypi.org/project/sel…). , if the latest version is 3.141.0, download:

Then go to the wheel file directory and install using PIP:

Pip3 install selenium - 3.141.0 - py2. Py3 - none - any. WHLCopy the code

4. Verify the installation

Enter the Python command line interaction mode and import the Selenium package. If no error is reported, the installation is successful:

$ python3
>>> import selenium
Copy the code

But this is not enough, because we also need to use a browser (such as Chrome, Firefox, etc.) to work with Selenium.

We will explain how to configure Chrome, Firefox, and PhantomJS. With a browser, we can work with Selenium to do page fetching.

The installation of the ChromeDriver

The Selenium library was successfully installed earlier, but it is an automated testing tool that requires a browser to work with. In this section, we introduce the Configuration of the Chrome browser and ChromeDriver.

First, download Chrome. There are many ways to do this, but I won’t go over them here.

Then install ChromeDriver. Chrome driver can be used to drive Chrome only after the ChromeDriver is installed. Here’s how to install ChromeDriver.

1. Related links

  • The official website: sites.google.com/a/chromium….
  • Download address: chromedriver.storage.googleapis.com/index.html

2. Preparation

Before installing Chrome, ensure that it is running properly. The installation process is not described in detail.

3. View the version

Click the Chrome menu “Help” → “About Google Chrome” to view the version number of Chrome:

Here my Chrome version is 92.0.

Remember the Chrome version number, as it will be used when selecting the ChromeDriver version.

4. Download the ChromeDriver

Open ChromeDriver’s official website:

  1. Sites.google.com/a/chromium….
  2. chromedriver.chromium.org/downloads
  3. chromedriver.storage.googleapis.com/index.html

Both links are ok.

5. Configure environment variables

Once the download is complete, configure the ChromeDriver executable to environment variables.

On Windows, it is recommended to drag the Chromedriver. exe file directly into the Python Scripts directory, as shown in the following figure:

Alternatively, you can configure its path to an environment variable separately.

On Linux and Mac, you need to configure the executable file to an environment variable or move the file to a directory that belongs to the environment variable.

For example, you want to move a file to the /usr/bin directory. First, you need to go to its path in command line mode, and then move it to /usr/bin:

sudo mv chromedriver /usr/bin
Copy the code

Of course, you can also configure ChromeDriver to $PATH. First, you can place the executable file in a directory of your choice, such as /usr/local/chromedriver. Then you can modify the ~ /. Profile file by running the following command:

export PATH= "$PATH:/usr/local/chromedriver"
Copy the code

Run the following command to save the configuration:

source ~/.profile
Copy the code

Environment variables can be added.

6. Verify the installation

After the configuration is complete, you can directly run the Chromedriver command on the CLI: ChromeDriver If the input console output is similar to the following figure, it indicates that the Chromedriver environment variables are configured.

Then test the program by executing the following Python code:

from selenium import webdriver
browser = webdriver.Chrome()
Copy the code

If a blank Chrome browser pops up after you run it, everything is fine. If no, check the previous configurations.

If the ChromeDriver version is incompatible with the ChromeDriver version, change the ChromeDriver version.

If there are no problems, then you can use Chrome to do web scraping.

AI Yue Chuang · Programming Private teacher: V: Jiabcdefh