Build Python 3.0 on linux server 7 + selenium + Firefox headless

Posted by Catharsis on Wed, 02 Feb 2022 18:19:20 +0100

1.1 build Python 3 seven

This article chooses to take Alibaba cloud server CentOS 7.6 operating system as an example
After tossing around for about a whole day, I found that basically all the python 3 installations are the most stable. However, I tried many articles on installing 3.7, which ended in failure. What they wrote was not bad. But what has fewer shortcomings is that it is almost the same, and it is a few steps away, but the article is there. It will lead to all kinds of bug s and errors. oh my god!!! Two words make a lot of noise

  1. Installation dependency. I was affected by this dependency for more than an hour.
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

  1. Replace wegt source with ALI source
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

Clear cache

yum clean all

Generate cache

yum makecache

Update the latest source settings

yum update -y

This step is basically no problem.. If something goes wrong, I really can't help it. Is to change the source and improve the download speed. I don't think there will be any problem with this. If it gets stuck, congratulations. I got stuck in the last step. No way, wait. Don't worry!!!

  1. Download Python 3.0 from wegt seven
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz

If the download is still slow after updating the source, go to the official Python 3 7 download it and transfer it to your server. It's all the same. I use wget just to pretend to be forced.

Look at the picture below: I also upload this package directly to the server.

  1. Unzip the tar package
tar -zxvf Python-3.7.0.tgz
  1. Enter the extracted directory and select the default installation. The default installation will install it under / usr/local/bin.
cd Python-3.7.0
./configure
make&&make install

If the error "ModuleNotFound: No module named '_ctypes'" is reported

Then enter Yum install libffi devel - y, and then enter make & & make install to install.

  1. For convenience, we can make a soft connection and let the default python and pip point to the newly installed python 3 and pip3
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3 /usr/bin/python

mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip3 /usr/bin/pip
  1. Configure yum < again. Since you set soft connection, the default python is not 2.7, but yum is supported by 2.7. Therefore, it must be clearly pointed out that version 2.7 >
vi /usr/libexec/urlgrabber-ext-down
vi /usr/bin/yum
vi /usr/bin/yum-config-manager

Modify the contents of the above three documents respectively. As shown in the figure below, we just need to add the string 2.7 after python.

If there is no / usr / bin / Yum config manager, we can write one ourselves. Or we can ignore it.

  1. Must be updated! to update! pip!
pip install --upgrade pip


2.1 pip install selenium directly

pip install selenium

3.1 Firefox headless

  1. I suggest that you download Firefox and geckodriver drivers on your own machine, and then upload them to the server.

You can choose any one. I chose it directly here Version 89.0.

You'd better choose the latest version of geckodriver


2. Just unzip it directly.

tar -jxvf Firefox-latest-x86_64.tar.bz2 
tar -zxvf geckodriver-v0.30.0-linux64.tar.gz 


  1. You must first install yum install gtk3 and run yum info gtk3, otherwise the browser may not run, and you need to do a fart automated test..
yum install gtk3
yum info gtk3

  1. Delete old versions of firefox
cd /usr/lib
ls | grep f*
rm -rf firefox-esr
rm -rf /usr/bin/firefox

If you don't have an old version of Firefox, don't delete it.. carry coals to newcastle.

  1. Move the unzipped Firefox directly to / usr/lib
mv firefox/ /usr/lib
  1. Create a new command
ln -s /usr/lib/firefox/firefox /usr/bin/firefox
  1. Add execution permission to the driver. This tm pit cost me about four hours
chmod +x geckodriver

No permission, of course Selenium is invalid..

  1. Just move the driver directly to / usr/bin, so you don't need to do any configuration
mv geckodriver /usr/bin/

4.1 prepare selenium Py script

from selenium import webdriver

# Try not to move the following parameters, otherwise an error will occur

options = webdriver.FirefoxOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')
browser = webdriver.Firefox(options=options)
#Try not to move the above parameters, otherwise an error will occur


browser.get('https://www.baidu.com')

print(browser.title)



success! success! success! Finish scattering flowers!

Topics: Linux Selenium server