Find Element by Attribute

Find an element in DOM based on an attribute value

Update: In the past few years the landscape has changed drastically. You can now reliably use querySelector and querySelectorAll, see Wojtek's answer for how to do this.

There's no need for a jQuery dependency now. If you're using jQuery, great...if you're not, you need not rely it on just for selecting elements by attributes anymore.


There's not a very short way to do this in vanilla javascript, but there are some solutions available.

You do something like this, looping through elements and checking the attribute

If a library like jQuery is an option, you can do it a bit easier, like this:

$("[myAttribute=value]")

If the value isn't a valid CSS identifier (it has spaces or punctuation in it, etc.), you need quotes around the value (they can be single or double):

$("[myAttribute='my value']")

You can also do start-with, ends-with, contains, etc...there are several options for the attribute selector.

Find element by attribute

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/css/2014/02/18/effective-css-selectors.html

Find element by the value attribute using Selenium Python

To locate the element through the value attribute i.e. 565657 you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "input[value*='565657']")
  • Using xpath:

    element = driver.find_element(By.XPATH, "//input[contains(@vlaue, '565657')]")

To locate ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[value*='565657']")))
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@vlaue, '565657')]")))
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

Get an element by using it's attribute value

Use this selector instead: [title="M\'affecter cette demande"]

var elem = document.querySelector('[title="M\'affecter cette demande"]');
console.log(elem);
<a href="javascript:void(0);" title="M'affecter cette demande" data-aura-rendered-by="1314:0" class="forceActionLink" data-aura-class="forceActionLink">      <div class="slds-truncate" title="M'affecter cette demande" data-aura-rendered-by="1315:0">M'affecter cette demande</div></a>

Selenium - how to find elements by attribute with any value in Java?

Just use //element[@someAttribute] for your XPath expression.

Is there a way to find an element by attributes in Python Selenium?

You can get it by xpath and check the node-type attribute value:

driver.find_element_by_xpath('//input[@node-type="searchInput"]')

jQuery, how to find an element by attribute NAME?

Simply use deleteuserid instead of deleteuserid!="" like following.

console.log($('[deleteuserid]'));


Related Topics



Leave a reply



Submit