Find element by attribute

You can easily get this task accomplished with CSS.

The formula is:

element[attribute="attribute-value"]

So if you have,

<a href="https://stackoverflow.com/questions/26304224/mysite.com"></a>

You can find it using:

By.cssSelector("a[href="https://stackoverflow.com/questions/26304224/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

Leave a Comment