Setting Up Automation Script with Selenium and Chrome CLI
This guide provides step-by-step instructions to set up an automation script using Python with Selenium and Google Chrome on a Linux environment.
Step 1: Install Google Chrome
-
Download the Google Chrome .deb package:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
-
Install the package:
sudo dpkg -i google-chrome-stable_current_amd64.deb sudo apt-get install -f
Step 2: Download ChromeDriver
-
Determine the version of Chrome you have installed:
google-chrome --version
Note the version number (e.g.,
114.0.5735.90
). -
Download the matching version of ChromeDriver from the ChromeDriver download page:
wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip unzip chromedriver_linux64.zip
Step 3: Move ChromeDriver to a Directory in Your PATH
-
Move the
chromedriver
binary to/usr/local/bin
:sudo mv chromedriver /usr/local/bin/ sudo chmod +x /usr/local/bin/chromedriver
-
Verify the installation:
chromedriver --version
Using ChromeDriver with Selenium
-
Install Selenium:
pip install selenium
-
Verify your installations:
chromedriver --version google-chrome --version ls -l /usr/local/bin/chromedriver
Steps to Resolve Common Issues
Verify the ChromeDriver File Location
-
Ensure you are pointing to the correct
chromedriver
binary:ls -l /home/ubuntu/.wdm/drivers/chromedriver/linux64/127.0.6533.88/
-
Ensure the binary is named
chromedriver
and has executable permissions:chmod +x /home/ubuntu/.wdm/drivers/chromedriver/linux64/127.0.6533.88/chromedriver
Update Your Script to Use the Correct Path
Ensure your script is pointing to the correct chromedriver
path. Using webdriver.Chrome()
with ChromeDriverManager
should automatically handle this. Ensure you are using the latest version:
pip install --upgrade webdriver_manager
Clear ChromeDriver Cache and Reinstall
-
Remove old ChromeDriver files:
rm -rf /home/ubuntu/.wdm/
-
Install ChromeDriver again using
webdriver_manager
.
Creating a Google Chrome Profile on Ubuntu for Selenium
Step 1: Create a New Chrome Profile
-
Launch Google Chrome and access profile management:
- Click on your profile icon in the upper right corner of the Chrome window and select "Add" to create a new profile.
- Choose a name and avatar for the profile and click "Add".
-
Locate Profile Directory:
ls ~/.config/google-chrome/ ls ~/.config/google-chrome/ | grep 'Profile'
Step 2: Configure Selenium to Use the Chrome Profile
-
Create a new directory for the profile:
mkdir -p ~/.config/google-chrome/NewProfile
-
Launch Chrome with the new profile:
google-chrome --user-data-dir=~/.config/google-chrome/NewProfile --profile-directory=Profile1
-
Close Chrome. The profile is now created and saved in
~/.config/google-chrome/NewProfile
.
Using Xvfb for a Virtual Framebuffer
If you need a graphical environment but don't have an actual display, use Xvfb (X virtual framebuffer) to emulate a display.
-
Install Xvfb:
sudo apt-get install xvfb
-
Run Xvfb in the background:
Xvfb :99 -screen 0 1024x768x24 &
-
Set the DISPLAY environment variable:
export DISPLAY=:99
-
Launch Chrome with the new profile:
google-chrome --user-data-dir=~/.config/google-chrome/pt-auto-profile --profile-directory=pt-auto-profile1
-
Run Selenium with the DISPLAY variable:
export DISPLAY=:99
Verifying File Permissions
- Verify the
chromedriver
file:file /usr/local/bin/chromedriver sudo chmod +x /usr/local/bin/chromedriver ls -l /usr/local/bin/chromedriver
By following these steps, you should be able to set up your automation script with Selenium and Chrome, resolving any issues related to
chromedriver
installation and configuration. https://chatgpt.com/share/d66b9e03-b408-43e6-a827-0955f55ee1ee (opens in a new tab)