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
Which Part of Throwing an Exception Is Expensive
Why Are Class Static Methods Inherited But Not Interface Static Methods
How to Use Concurrentlinkedqueue
When Should an Illegalargumentexception Be Thrown
Hashmap Implementation in Java. How Does the Bucket Index Calculation Work
Sslhandshakeexception: No Subject Alternative Names Present
How to Compile Java to Native Code
Is There a Java Library to Access the Native Windows API
How to Configure Log4J with a Properties File
Find the Most Popular Element in Int[] Array
JSON Consumer of Polymorphic Objects
How to Get the Count of Line in a File in an Efficient Way
Why Is System.Arraycopy Native in Java
Using Eclipse Java Compiler (Ecj) in Maven Builds
Error: Java_Home Is Not Defined Correctly Executing Maven
Java: Subtract '0' from Char to Get an Int... Why Does This Work