54
votes

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;
    }
    else{
        return null;
    }
}

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

4
Could you add an HTML example of what you're trying to match? - Richard
Why does XPath not help? - Simon Fischer
Your question seems to be about matching attribute, but your example is matching the text of the element, nothing to do with attributes. - Richard
Well, This application will sometimes be used by non developers, and if they can't trace out XPath, I need them to use any of the attributes available for particular element. I already had working versions of find by - Id, CSS, tagName, XPath and all. And let's say we need to find an Image with attribute <href> and value as <url>, and assuming there are no Id, class name etc mentioned (I am trying to figure out on worst cases), How to get element with just href and url? - Kishore Banala
I believe that "Assuming XPath is not an option" is a valid assumption. It's very frequent that the answers criticize the question and actually don't answer it. It's like when you try to buy a warm jacket during the summer and the sales person questions why you're doing that and tries to sell you a thin T-shirt. Usually, you feel you don't need to explain your life or your reasons and just need a warm jacket. Are you buying from this person? In short: I believe that first we have to respect the person who's asking, try to answer the question, and only then offer other options. - sflr

4 Answers

100
votes

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

35
votes

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']"))
15
votes

Use CSS selectors instead:

List<WebElement> elements = webDriver.findElements(By.cssSelector("*[attributeName='value']"));

Edit: CSS selectors instead of XPath

0
votes

As per the documentation:

By.id Locates elements by the ID attribute. This locator uses the CSS selector *[id='$ID'], not document.getElementById. Where id is the ID to search

so you can use the below code to search the DOM element with any given attribute as ID and value

By.id("element[attribute='attribute-value']");