67

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?

Kishore Banala
  • 946
  • 1
  • 8
  • 12
  • Could you add an HTML example of what you're trying to match? – Richard Oct 10 '14 at 16:48
  • Why does XPath not help? – Simon Fischer Oct 10 '14 at 16:48
  • Your question seems to be about matching attribute, but your example is matching the text of the element, nothing to do with attributes. – Richard Oct 10 '14 at 16:48
  • 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 and value as , 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 Oct 10 '14 at 18:01
  • 4
    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. – Almir Campos Mar 17 '19 at 17:10

4 Answers4

112

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

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ddavison
  • 28,221
  • 15
  • 85
  • 110
38

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']"))
SiKing
  • 10,003
  • 10
  • 39
  • 90
  • Downvoted because the question declared explicitly that "Xpath is not an option". So, the answer doesn't match the question's requirement. – Almir Campos Mar 17 '19 at 17:13
  • 5
    @AlmirCampos nevertheless, I find the answer useful. I ask myself: does this answer help me, and will it help others on SO? For me, the answer is 'yes'. – Holf Sep 06 '19 at 15:36
  • Worked for non-standard element and was exactly what I didn't know I needed: `const arasUserMenuDD = await driver.findElements(By.xpath("//aras-dropdown[@data-id='com.aras.innovator.cui_default.mwh_user_menu']"`));` – Neil Gaetano Lindberg Apr 16 '20 at 18:44
17

Use CSS selectors instead:

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

Edit: CSS selectors instead of XPath

Sizik
  • 1,066
  • 5
  • 11
1

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']"); 
Irshad
  • 1,016
  • 11
  • 30