With Selenium ready and a solution in place to download drivers with ease, we now can start with the interesting part and automate our browser.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Preparation
Make sure that you have Selenium installed on your machine and that you use the download manager to get the right drivers.
Navigate to a site
We can tell Selenium to navigate to a web site with the get() method:
1 |
driver.get("https://duckduckgo.com/?t=ha&va=j") |
When the browser got the page we requested, we can start to interact with it and ask for its title:
1 |
title = driver.title |
This gives us the title of the page and we can make sure that we are at the right place:
DuckDuckGo — Privacy, simplified.
Find elements
We can find all elements of a page with a combination of the right selectors. First, we need to import the By module to get access to the selectors:
1 |
from selenium.webdriver.common.by import By |
This import allows us to search elements by different approaches:
- By.ID – search for the id attribute
- By.TAG_NAME – search for the HTML tag name of an element
- By.CLASS_NAME – search for the class name of an element
- By.LINK_TEXT – search using the exact link text
- By.PARTIAL_LINK_TEXT – search using parts of the link text
- By.NAME – search in the name attribute of an element
- By.XPATH – search with an XPath expression
- By.CSS_SELECTOR – search with the CSS selector syntax
To get all elements with an H1
tag, we can use the method find_elements() method to find them and show the text between the H1 tags with this code:
1 2 3 |
headers = driver.find_elements(by=By.TAG_NAME, value="H1") for header in headers: print(f"H1: [{header.text}]") |
Unfortunately for us, there are plenty of H1
in the site and most have no text:
H1: []
H1: []
H1: []
H1: [Tired of being tracked online? We can help.]
H1: [We don’t store your
personal information. Ever.]
H1: []
H1: []
H1: []
H1: []
If we know that there is only ever one element or we are just interested in the first one, we can use the find_element() method (singular):
1 |
logo = driver.find_element(by=By.ID, value="logo_homepage_link") |
Unlike to C#
, the find_element() method does not throw an exception if it finds multiple matching elements.
Fill input forms
We can use the selectors to get an input form element and write text to it with the send_keys() method:
1 2 3 4 |
search_box = driver.find_element( by=By.ID, value="search_form_input_homepage") search_box.send_keys("Selenium web") |
We can see the filled-out search form in the Browser:
To fill out a whole form, we need to repeat this for each field.
Click buttons
To submit a form, we need to find the submit button and click it:
1 2 3 4 |
search_button = driver.find_element( by=By.ID, value="search_button_homepage") search_button.click() |
Next
This covers the most important parts to automate your browser. As soon as you try that on a real web page, you will notice that this is not quite enough to tackle all edge cases. Thus, next week we look at some ways to mitigate the most common obstacles.
1 thought on “Python Friday #144: Automate Browsers With Selenium (Part 1)”