You can easily accomplish this task with CSS.

The formula is:

element[attribute='attribute-value']

So if you have,

<a href="mysite.com"></a>

You can find it using:

By.cssSelector("a[href='mysite.com']");

this works using any attribute possible.

This page here gives good information on how to formulate effective css selectors, and matching their attributes: http://ddavison.io/2014/02/18/effective-css-selectors

Answer from ddavison on Stack Overflow
🌐
Selenium Python
selenium-python.readthedocs.io › locating-elements.html
4. Locating Elements — Selenium Python Bindings 2 documentation
from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]') driver.find_elements(By.XPATH, '//button') The attributes available for the By class are used to locate elements on a page.
Discussions

Python - Selenium get Element by attribute and the full attribute value - Stack Overflow
Hi how can I get an element by attribute and the attribute value in Python Selenium? For example I have class="class1 class2 class3". Now I want to get the element with the attribute clas... More on stackoverflow.com
🌐 stackoverflow.com
Is there a way to find an element by attributes in Python Selenium? - Stack Overflow
I have checked the selenium (python) ... elem by the node-type attr. Is there a explicit way to locate this elem in python selenium? ... Sign up to request clarification or add additional context in comments. ... CSS Selectors notation for attribute is: tag_name[attribute_name=attribute_value]. Simpler and faster than XPath 2018-11-26T20:54:22.137Z+00:00 ... AttributeError: 'WebDriver' ... More on stackoverflow.com
🌐 stackoverflow.com
[Python 3 + Selenium] - Finding an element by xpath and text in the href attribute.
I had a similar problem earlier today. https://www.reddit.com/r/learnpython/comments/4282ip/understanding_selenium_nonbreakage/ I don't understand why it made it stop breaking (but I asked here and nobody who knows must have seen it yet) but I added a general "find" like this for your example driver.find_elements_by_xpath("//a[@href='javascript:__doPostBack('ctl00$middle$gvEmployers','Page$%s')']" %page) before the variable assignment page_link = driver.find_element_by_xpath("//a[@href='javascript:__doPostBack('ctl00$middle$gvEmployers','Page$%s')']" %page) I don't know if that will fix it for you because I don't understand it, but maybe try that. Note the "s" on driver.find_elements More on reddit.com
🌐 r/learnpython
8
4
January 24, 2016
Is there a list of attributes that can be specified with Selenium get_attribute()?
I"m not sure what list you would want. You use it to get any of the attributes that are in the HTML element you are referencing. And I don't understand your second question either; what does "retrieve text but include href" mean? What would the result look like? More on reddit.com
🌐 r/learnpython
4
1
August 28, 2022
🌐
Baeldung
baeldung.com › home › testing › finding element by attribute in selenium
Finding Element by Attribute in Selenium | Baeldung
March 29, 2024 - We can locate an element by using its class as an attribute. This is a common technique, especially when checking if an element is enabled, disabled, or has gained some other capability reflected in its class.
🌐
Selenium
selenium.dev › documentation › webdriver › elements › finders
Finding web elements | Selenium
September 6, 2025 - One solution is to locate an element with a unique attribute that is an ancestor of the desired element and not an ancestor of the undesired element, then call find element on that object: Move Code · Java · Python · CSharp · Ruby · JavaScript ...
🌐
BrowserStack
browserstack.com › home › guide › getattribute() method in selenium [2026]
getAttribute() method in Selenium: What, Why, and How to use | BrowserStack
December 26, 2025 - By calling getAttribute() on a WebElement and passing the attribute name as a parameter, Selenium returns the corresponding value as a string, enabling accurate verification of element properties during test execution.
🌐
TutorialKart
tutorialkart.com › selenium › java › find-elements-by-attribute-value
Find Element(s) by Attribute - Selenium Java
June 23, 2022 - To find the element(s) by the value of an attribute using Selenium in Java, use WebDriver.findElement(By.xpath()) or WebDriver.findElements(By.xpath()) and pass the expression with the attribute and value as argument to the xpath().
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › is-there-a-way-to-find-an-element-by-attributes-in-python-selenium
Is there a way to find an element by attributes in Python Selenium?
September 18, 2020 - from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/tutor_connect/index.php") #identify element with attribute id l= driver.find_element_by_xpath("//input[@id='txtSearchText']") l.send_keys("Tutorialspoint") #get_attribute() to get value of input box print("Enter text is: " + l.get_attribute('value')) driver.close()
🌐
LambdaTest
lambdatest.com › blog › selenium-getattribute
How To Get Attribute Value In Selenium WebDriver? | LambdaTest
April 15, 2024 - Now that you know all about an attribute for an HTML web element and what it looks like let us see how we can fetch the attribute values with the help of getAttribute() in Selenium WebDriver.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get_attribute-element-method-selenium-python
get_attribute() element method - Selenium Python - GeeksforGeeks
July 12, 2025 - Let's start by retrieving the href attribute of a link element on the GeeksforGeeks homepage. ... from selenium import webdriver # Create WebDriver object driver = webdriver.Firefox() driver.get("https://www.geeksforgeeks.org/") el = driver.find_element("link text", "DSA") print(el.get_attribute("href"))
🌐
Selenium
selenium.dev › documentation › webdriver › elements › locators
Locator strategies | Selenium
February 16, 2026 - We can use css selector locator strategy to identify the element on the page. If the element has an id, we create the locator as css = #id. Otherwise the format we follow is css =[attribute=value] .
🌐
Testim
testim.io › blog › selenium-get-attribute
getAttribute() in Selenium Web Driver: Get Started In 5 Steps
November 11, 2022 - Selenium get attribute method allows users to get the value of an element's attribute. Learn all about it in today's post.
🌐
Selenium
selenium.dev › selenium › docs › api › java › org › openqa › selenium › WebElement.html
Interface WebElement
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead. See W3C WebDriver specification for more details. ... The first matching element on the current context. ... A representation of an element's shadow root for accessing the shadow DOM of a web component. ... Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › WebDriver › Reference › Commands › GetElementAttribute
Get Element Attribute - WebDriver - MDN Web Docs
April 28, 2025 - from selenium import webdriver session = webdriver.Firefox() session.get("https://google.com/?hl=en") search_box = session.find_element_by_id("q") print(search_box.get_attribute("id")) Output: q · Was this page helpful to you? Yes · No Learn how to contribute ·
🌐
Quora
quora.com › Is-there-a-way-to-locate-an-element-using-Selenium-where-the-element-uses-a-custom-attribute
Is there a way to locate an element using Selenium, where the element uses a custom attribute? - Quora
Answer (1 of 4): Most selenium implementations such as behat/mink, codeception, phantomjs support CSS querys to locate elements. Say for instance you have an element like this: You can locate it with this query: [data-value] If you want to find custom attribute...
🌐
TutorialsPoint
tutorialspoint.com › how-to-find-an-element-using-the-attribute-name-in-selenium
How to find an element using the attribute “name” in Selenium?
April 3, 2021 - findElement(By.cssSelector("input[name='q']")); String st = p.getAttribute("value"); System.out.println("Attribute value: " + st); //identify element with xpath WebElement o = driver.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-find-an-element-using-the-attribute-class-name-in-selenium
How to find an element using the attribute “class name” in Selenium?
November 8, 2023 - findElement(By.cssSelector("input[class='input']")); String str = n.getAttribute("value"); System.out.println("Attribute value is : " + str); //identify element with xpath WebElement p = driver.
🌐
Testup
testup.io › how-to-get-attribute-of-element-from-selenium
How to Get Attribute of Element from Selenium? – testup.io
In Selenium webdriver, we can use the getAttribute() method to get the value of an attribute.
🌐
Testsigma
testsigma.com › blog › test automation › how to find element by text in selenium: tutorial with example
How to Find Element by Text in Selenium: Tutorial with Example
October 24, 2024 - In Selenium, Find Element by Text locates a web element based on its text attribute. We'll see how to use the text() method.