Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
click() element method - Selenium Python
Next article icon

Selenium is a powerful Python module used for browser automation. It allows you to interact with web pages just like a real user-click buttons,fill forms, and fetch values from elements.

The get_attribute()method fetches the value of an element’s HTML attribute.

In this article, we'll learn how to use theget_attribute() method in Selenium to extract an element’s attribute value (like href, id, placeholder, etc).

Syntax

element.get_attribute("attribute_name")

Parameters:

Return Type: the attribute value as a string, or None if the attribute is not found.

Installation

Install the selenium module using pip, use this command:

pip install selenium

Examples of get_attribute()

Example 1: Get href from a Link

Let's start by retrieving the href attribute of a link element on the GeeksforGeeks homepage.

Python
fromseleniumimportwebdriver# Create WebDriver objectdriver=webdriver.Firefox()driver.get("https://www.geeksforgeeks.org/")el=driver.find_element("link text","DSA")print(el.get_attribute("href"))

Terminal Output:

seleniium1111122222
Terminal output

Outputafter visiting the URL in the terminal:

seleniium1111111111111
DSA Tutorial page

Explanation:

  • get() method opens the GeeksforGeeks homepage.
  • find_element() method finds the Courses link using link text locator.
  • get_attribute('href') fetches the destination URL of the anchor (<a>) tag.

Example 2: Get placeholder Attribute from an Input Field

In this example, we’ll extract the placeholder text from the Google search input box.

Python
fromseleniumimportwebdriver# Initialize Firefox WebDriverdriver=webdriver.Firefox()driver.get("https://www.google.com/")box=driver.find_element("name","q")print(box.get_attribute("placeholder"))

Explanation:

  • get() method navigates to Google's homepage.
  • find_element("name", "q") locates the search input box using the name locator.
  • get_attribute("placeholder")returns the default placeholder text, such as"Search".

Other Locator Examples

You can also locate elements using other strategies likeidorxpath.

Python
el=driver.find_element("id","link")el=driver.find_element("xpath","//a[@id='link']")

Explanation:

  • find_element("id", ...) locates elements using the id attribute.
  • find_element("xpath", ...) allows more complex querying via XPath.
  • These elements can then be passed toget_attribute() for attribute extraction.

Example 3: Get href of Multiple Anchor Tags

Here’s how to extract the href attribute from all anchor (<a>) tags on a page.

Python
fromseleniumimportwebdriverdriver=webdriver.Firefox()driver.get("https://www.geeksforgeeks.org/")links=driver.find_elements("tag name","a")forlinkinlinks:print(link.get_attribute("href"))

Output:

seleniium1111133333333333333
Terminal output of all the hrefs

Explanation:

  • get() method opens the GeeksforGeeks site.
  • find_elements() returns a list of all anchor tags (<a>).
  • get_attribute("href") fetches each link's destination.

Related Articles:


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp