The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".
Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.
The querySelectorAll() method in HTML is used to return a collection of an element's child elements that match a specified CSS selector(s), as a static NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers.
You can easily get this task accomplished 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
I do not understand your requirement:
Assuming XPath is not an option ...
If this was just an incorrect assumption on your part, then XPath is the perfect option!
webDriver.findElements(By.xpath("//element[@attribute='value']"))
Of course you need to replace element
, attribute
, and value
with your actual names. You can also find "any element" by using the wildcard:
webDriver.findElements(By.xpath("//*[@attribute='value']"))
Use CSS selectors instead:
List<WebElement> elements = webDriver.findElements(By.cssSelector("*[attributeName='value']"));
Edit: CSS selectors instead of XPath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With