How can I get the href of elements found by partial link text using Selenium?

SeleniumAutomation TestingTesting Tools

We can get the href of elements found by partial link text with Selenium webdriver. First of all we need to identify the links with help of the find_elements_by_partial_link_text() method.

Next to get hold of href of the links we have to use the get_attribute() method and then pass href as a parameter to the method.

Let us identify the href attribute of links identified with partial link text Policy in the below page.

Example

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify links with partial link text
l= driver.find_elements_by_partial_link_text("Policy")
#iterate links
for i in l:
#get href from get_attribute()
print("Href value of link: " + i.get_attribute("href"))
driver.close()

Output

raja
Published on 18-Sep-2020 17:39:27
Advertisements