Selenium 4 / Python

t00m profile image Tomás Vírseda ・2 min read

Hi there,

Yesterday, after more than one year without working in one of my pet projects, I cloned the repository in a new Ubuntu desktop 20.04

Most of the GUI code worked out, but it wasn't the case for the Selenium bits. So I decided to take a closer look, and then, I realized that Selenium python bindings had been updated from the stable version 3.141.0 to the new pre-release 4.0.0a5

After many tries, I was only getting these messages:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

DeprecationWarning: firefox_profile has been deprecated, please pass in a Service object

So, I took a look to the source code and, finally, I was able to solve the issue:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

# Options
options = Options()

## Option for using a custom Firefox profile
options.profile = '/home/t00m/.basico/opt/webdrivers/basico.default'

## Enable headless
options.headless = False

# Specify custom geckodriver path
service = Service('/home/t00m/.basico/opt/webdrivers/geckodriver')

# Test
browser = webdriver.Firefox(options=options, service=service)

Bonus track

My application needs to load a page via SSO. In the past, I used a timeout. Now, I keep the timeout but I've added the following code to skip it if it loads the page faster:

# Extra imports
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Test
timeout = 10
browser = webdriver.Firefox(options=options, service=service)

    element_present = EC.presence_of_element_located((By.ID, 'snack-zone'))
    WebDriverWait(browser, timeout).until(element_present)
except TimeoutException:
    print("Timed out waiting for page to load")
    print("Page loaded")

Take care. Stay safe.

Posted on May 27 by:

t00m profile

Tomás Vírseda


SAP Basis Consultant. Non-professional developer. I love to read about metadata and Semantic Web.


markdown guide