Selenium

Selenium#

selenium allows you to use software code to analyse the behaviour of browsers. It’s realised for a few programming languages but this page is focused on using it with python.

Versions#

If you want Selenium to work with Firefox, you need to have the appropriate versions of geckodriver, selenium and firefox installed. You can find the correct combinations here.

In the following cell is some code that will allow you to find your current versions. And as my output here shows, all the examples in this section worked fine.

import os
import subprocess
import selenium
output = subprocess.run(['geckodriver', '-V'], stdout=subprocess.PIPE, encoding='utf-8')

print("=========geckodriver==============")
print(output.stdout)
print("=========selenium version==============")
print(selenium.__version__)
print("=========firefox===============")
out = os.system("firefox --version")
=========geckodriver==============
geckodriver 0.34.0 ( 2024-03-22)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

=========selenium version==============
4.11.2
=========firefox===============
Mozilla Firefox 124.0.1

Selenium using example#

There is basic example of using selenum on official website. Looks like it just load page press “sumbit” button and exit. However, it has been modified to be more suitable for Jupyter Notebook - the code in the following cell takes a screenshot of the page and displays it as the cell’s output.

Note there are may be some differences associated with browser you’re using. Here is examples for firefox. In this example for webriver, you need to specify the service.executable_path as the path for geckodriver - there’s no information about this in the official documentation page.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from PIL import Image
import io

# Set up Firefox options
options = Options()
options.binary_location = "/usr/bin/firefox"

driver = webdriver.Firefox(
    options=Options(),
    service=Service(
        # path to the geckodriver on my computer
        executable_path="/snap/bin/geckodriver",
        log_output="/dev/null"
    )
)
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
driver.implicitly_wait(0.5)
screenshot = driver.get_screenshot_as_png()
image = Image.open(io.BytesIO(screenshot))
display(image)

driver.quit()
../_images/1b142897bf63f371c4dc9e05c8f2b741aeb25298d6073f958bbe770a66adac6b.png