How to click on a link using Selenium webdriver in Python.


We can click on a link using Selenium webdriver in Python. A link is represented by the anchor tag. A link can be identified with the help of the locators like - link text and partial link text.

We can use the link text attribute for an element for its identification and utilize the method find_element_by_link_text. With this, the first element with the matching value of the given link text is returned.

Syntax

driver.find_element_by_link_text("value of link text")

We can also use the partial link text attribute for an element for its identification and utilize the method find_element_by_partial_link_text. With this, the first element with the matching value of the given partial link text is returned.

For both the locators, if there is no element with the matching value of the partial link text/link text, NoSuchElementException shall be thrown.

Syntax

driver.find_element_by_partial_link_text("value of partial ink text")

Let us see the html code of a webelement −

The link highlighted in the above image has a tagname - a and the partial link text - Refund. Let us try to click on this link after identifying it.

Example

Code Implementation

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify link with partial link text
l = driver.find_element_by_partial_link_text('Refund')
#perform click
l.click()
print('Page navigated after click: ' + driver.title)
#driver quit
driver.quit()

Output

Updated on: 19-Nov-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements