Linux
OS-Mgmt
Chrome CLI Selenium

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

  1. Download the Google Chrome .deb package:

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  2. Install the package:

    sudo dpkg -i google-chrome-stable_current_amd64.deb
    sudo apt-get install -f

Step 2: Download ChromeDriver

  1. Determine the version of Chrome you have installed:

    google-chrome --version

    Note the version number (e.g., 114.0.5735.90).

  2. 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

  1. Move the chromedriver binary to /usr/local/bin:

    sudo mv chromedriver /usr/local/bin/
    sudo chmod +x /usr/local/bin/chromedriver
  2. Verify the installation:

    chromedriver --version

Using ChromeDriver with Selenium

  1. Install Selenium:

    pip install selenium
  2. Verify your installations:

    chromedriver --version
    google-chrome --version
    ls -l /usr/local/bin/chromedriver

Steps to Resolve Common Issues

Verify the ChromeDriver File Location

  1. Ensure you are pointing to the correct chromedriver binary:

    ls -l /home/ubuntu/.wdm/drivers/chromedriver/linux64/127.0.6533.88/
  2. 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

  1. Remove old ChromeDriver files:

    rm -rf /home/ubuntu/.wdm/
  2. Install ChromeDriver again using webdriver_manager.

Creating a Google Chrome Profile on Ubuntu for Selenium

Step 1: Create a New Chrome Profile

  1. 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".
  2. Locate Profile Directory:

    ls ~/.config/google-chrome/
    ls ~/.config/google-chrome/ | grep 'Profile'

Step 2: Configure Selenium to Use the Chrome Profile

  1. Create a new directory for the profile:

    mkdir -p ~/.config/google-chrome/NewProfile
  2. Launch Chrome with the new profile:

    google-chrome --user-data-dir=~/.config/google-chrome/NewProfile --profile-directory=Profile1
  3. 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.

  1. Install Xvfb:

    sudo apt-get install xvfb
  2. Run Xvfb in the background:

    Xvfb :99 -screen 0 1024x768x24 &
  3. Set the DISPLAY environment variable:

    export DISPLAY=:99
  4. Launch Chrome with the new profile:

    google-chrome --user-data-dir=~/.config/google-chrome/pt-auto-profile --profile-directory=pt-auto-profile1
  5. Run Selenium with the DISPLAY variable:

    export DISPLAY=:99

Verifying File Permissions

  1. 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)


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!