Wednesday, September 28, 2022

run Selenium Python Script with Headless Chrome

Installing Selenium and Webdriver for Python

We will use a virtual environment for running Python scripts. Follow the below steps to create Python virtual environment and install the required python modules.

  1. Create a directory to store Python scripts. Then switch to the newly-created directory.
    mkdir tests && cd tests 
    
  2. Set up the Python virtual environment and activate it.
    python3 -m venv venv 
    source venv/bin/activate 
    

 

Once the environment is activated, You will find the updated prompt as shown below screenshot:

Create Python Environment for Selenium on Ubuntu
Create Python Environment for Selenium on Ubuntu
Now use PIP to install the selenium and webdriver-manager Python modules under the virtual environment.
pip install selenium webdriver-manager  
 Installing Selenium and Webdriver Python Module on Ubuntu & DebianInstalling Seleniu

 

 

Selenium Python Script with Headless Chrome

 

 Now, create a sample selenium script in Python that fetches the title of a website.

This script will run headless, So you can run it without an X desktop environment. You can simply SSH to your system and run the below example. Make sure Chromedriver is install in arm64 type. See my other blog

  1. Create a Python script and edit it in your favorite text editor:
    nano test.py 
    
  2. Copy-paste the following Selenium Python script to the file.

    ====

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    #from webdriver_manager.chrome import ChromeDriverManager
     
    chrome_driver_path="/usr/bin/chromedriver"
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    #driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
     
    driver= webdriver.Chrome(executable_path=chrome_driver_path, options=options)
    driver.get("https://python.org")
    print(driver.title)
    driver.close()

     ====

     

    Press CTRL + O to save content to file and press CTRL + X to close editor.

  3. Now, run this Python script in a shell.
    python test.py 
    

    You will see the output something like below:

    Running Selenium Python Script in Ubuntu & Debian
    Running the Selenium Python Script

How to install Chromedriver in Arm64 ubuntu linux?

# sudo apt update

# sudo apt-get install chromium-chromedriver