Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I am trying to find an element with Attribute. Well, I can find elements with Id, tagName, Xpath and all other predefined methods in Selenium. But, I am trying to write a method that specifically returns WebElement, given Attribute name and value as input.

List<WebElement> elements = webDriver.findElements(By.tagName("Attribute Name"));

for(WebElement element : elements){

    if(element.getText().equals("Value of Particular Attribute")){

        return element;



        return null;



Assuming XPath is not an option, is there any other better ways to do this?

1 Answer

0 votes
by (62.9k points)

You can easily get this task accomplished with CSS.

The formula is:


So if you have,

<a href=""></a>

You can find it using:


this works using any attribute possible.

Check out the below link which tells how to formulate effective CSS selectors and matching their attributes:

I hope it helps!

Browse Categories