Selenium is a framework used for testing web applications. It is very efficient and allows browsers to carry out multiple tests.
Selenium uses locators to uniquely identify web elements. There are various locators such as ID
, name
, CSS Selector
, and XPath
. To locate links on a text page, we can use a link Text locator.
Link Text is used to identify <a>
tags and can, therefore, extract the hyperlinks from within them. The following code suggests how links can be extracted from within the attached webpage.
Consider the following webpage:
<html>
<head>
<title>Educative.io</title>
<body>
<a href = "https://educative.io"> Visit </a>
<a href = "https://www.google.com"> Visit </a>
</body>
</html>
You can use the following to collect the link for the href on the word “Visit” :
visit_link = driver.find_element_by_link_text('Visit')
Note: Since there are two hyperlinks on similar words, it will return the first hyperlink to the word “Visit”.
Link Text is used to identify <a>
tags and can, therefore, extract the hyperlinks from within them. Partial Link Text can only be used when a part of the word is added, which enables quicker retrieval for the users.
Consider the following webpage:
<html>
<head>
<title>Educative.io</title>
<body>
<a href = "https://educative.io"> Visit </a>
<a href = "https://www.google.com"> Google </a>
</body>
</html>
You can use the following to collect the link for the href on the word “Visit” :
visit_link = driver.find_element_by_partial_link_text('Vi')
Note: If there are two hyperlinks on similar words, it will return the first hyperlink to the word that contains “Vi”.
Free Resources