How to Click a Link on a Web Page using the Selenium Module in Python



Python


In this article, we show how to click a hypertext link on a web page using the selenium module in Python.

In order to run the program on this page, you will need to install the selenium module, which you can do with the line, pip install selenium. You also will need to install either the geckodriver (if you are working with the Firefox web browser) or the ChromeDriver (if you are working with the Chrome web browser).

So in order to click a link on web page, you need to be able to first identify which link you want to click.

This can be done in a few ways.

Using selenium, we can find elements by class, by css selector, by id, by link text, by partial link text, by name, or by tag name.

This is shown in the table below.

Selenium's WebDriver Methods for Finding Elements
Method name Targeted Web Element
browser.find_element_by_class_name(name) Finds elements that matches the class attribute
browser.find_elements_by_class_name(name)


browser.find_element_by_id(id) Finds elements that matches the id attribute
browser.find_elements_by_id(id)


browser.find_element_by_tag_name(name) Finds elements that matches the tag name; an a or A matches an anchor element (element specified is case insensitive)
browser.find_elements_by_tag_name(name)


browser.find_element_by_name(name) Find elements that matches the name attribute
browser.find_elements_by_name(name)


browser.find_element_by_css_selector(selector) Finds elements that maches the CSS selector
browser.find_elements_by_css_selector(selector)


browser.find_element_by_link_text(text) Finds elements that completely matches the text specified
browser.find_elements_by_link_text(text)


browser.find_element_by_partial_link_text(text) Finds elements that contain the text specified
browser.find_elements_by_partial_link_text(text)


So there are many ways of targeting an HTML elements such as a link using functions in selenium.

However, one of the greatest ways of doing it is just by the text of the hyperlink.

In the code below, we will click a link on a web page by the text of the link.

As shown in the table above, this can be done using 1 of 2 functions. Either the browser.find_element_by_link_text() can be used to completely match text that you specify or the browser.find_element_by_partial_link_text() can be used to find a match containing the text provided.

In the code below, we go to Amazon's website and click on the link containing the text 'New Releases'.



So the first thing we have to do is import webdriver from the selenium module.

We use Firefox in this case, so we set the variable, browser, equal to, webdriver.Firefox()

We then use the get() function to retrieve the home page of amazon.

We then create another variable, new_releases, which finds the link by the text of the link. In this case, the text is 'New Releases'. It looks for the hyperlink that has the complete text, 'New Releases'

The line, new_rleases.click(), clicks on the link containing the text 'New Releases'.

You could also find this text using the browser.find_element_by_partial_link_text() function.

In this case, since this function only needs partial text, we specify 'Releases' only, and we will find that it finds the same link and clicks it.

This is shown in the code below.



This code achieves the same as the code before because it only needs partial text in order to find the link instead of the complete text within the anchor tags.

And this is how we can click a link on a web page using the selenium module in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...