How can I get the href in a <a class in Python Selenium?

How can I get the href in a <a class in Python Selenium?
How can I get the href in a <a class in Python Selenium?

You can use the find_element_by_css_selector method in Selenium to locate the <a> element by its class, and then use the get_attribute method to retrieve the href attribute.

Here is an example:

from selenium import webdriver

# create a new webdriver instance
driver = webdriver.Firefox()

# navigate to a website
driver.get("https://www.example.com")

# locate the <a> element by class
link = driver.find_element_by_css_selector(".classname")

# get the href attribute
href = link.get_attribute("href")

# print the href
print(href)

# close the browser
driver.quit()

You can replace .classname with the classname of your desired <a> tag and it will give you the href of that <a> tag.

Leave a Reply