DEV Community

Cover image for Browser automation with Python
tcs224
tcs224

Posted on

Browser automation with Python

You can automate a web browser with Python using the selenium module. The selenium module lets you control Google Chrome, Mozilla Firefox and other browsers.

First, what is selenium?

What is Selenium?

Selenium automates web browsers. There are bindings for Python, that lets you use selenium using Python code. The web browser can run on the same computer or remote (another computer or mobile device).

Mainly it is for automating web applications for testing, but is certainly not limited to just that. The selenium module lets you

  • do automated testing
  • simulate web surfing
  • do web scraping
  • do usability testing

Before trying selenium, you should know the basics of Python

To use selenium, you should install both the selenium module and the selenium web driver.

selenium web driver

Example

The program below starts the Chromium browser from a Python script, types the website url itself in the browser and loads it. Finally it outputs the html source of the web page.

from selenium import webdriver
import time

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')

html = driver.page_source
print(html)

(from selenium get source)

If you prefer to use Firefox, just change the driver to firefox. Make sure you have the web driver installed otherwise it will not work.

# coding=utf-8
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://python.org")

Keep in mind that the version of the web driver must be intended for the version of the web browser. A new web browser version, means you must update the driver.

Once you have selenium working on your computer, you can do anything you can do on the web from Python,fully automated, and more like take screenshots or execute javascript on the same page..

Related links:

Top comments (0)