How to directly find WebElements by their attributes except "class" and "name" (for example "title")

21,067

There are many methods while archiving an element by XPath

1 Absolutely path

<html>
  <body>
     <div>
       <form>
          <input id="demo"/>
       </form>
     </div>
   </body>
 <html>

To get the 'input' tag

xpath="/html/body/div/form/input"

2 Relative path

<html>
  <body>
     <div>
       <form>
          <input id="demo"/>
       </form>
     </div>
   </body>
 <html>

To get the 'input' tag

xpath="//input"  

3 Index

<html>
  <body>
     <div>
       <form>
          <input id="demo1"/>
          <input id="demo2"> 
       </form>
     </div>
   </body>
 <html>

To get the input 'demo2'

xpath="//input[1]"

4 Arbitrary single attribute

<html>
  <body>
     <div>
       <form>
          <input id="demo1"/>
          <input id="demo2" foo="bar"> 
       </form>
     </div>
   </body>
 <html>

To get input 'demo2'

xpath="//input[@id='demo2']" (equivalent to By.id)

Or

xpath="//input[@foo='bar']"

5 Arbitrary multiple attributes

<html>
    <body>
     <div>
       <form>
          <input id="1" type="submit" />
          <input id="2" foo="bar"/>
          <input id="3" type="submit" foo="bar"/> 
       </form>
     </div>
   </body>
 <html>

To get 3rd input

xpath="//input[@type='submit'][@foo='bar']"

Or

xpath="//input[@type='submit' and @foo='bar']"

If use xpath="//input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.

6 Contains attribute

<html>
    <body>
     <div>
       <form>
          <input id="1" type="submit" />
          <input id="2" foo="bar" daddy="dog"/>
          <input id="3" type="submit" foo="bar"/> 
       </form>
     </div>
   </body>
 <html>

To get the second input

xpath="//input[@daddy]"

Because only the second one has attribute 'daddy'

7 Inner searching

 <html>
    <body>
     <div>
       <form>
          <input id="input1" daddy="dog" />
          <input id="input2" daddy="pig"/>
       </form>
     </div>
     <div>
       <form>
          <input id="input3" daddy="dog" />
          <input id="input4" daddy="apple"/>
       </form>
     </div>
   </body>
 <html>

To get the second div

xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"

Overall those are all I can work out for now. Hope it helps.

Share:
21,067
LoveLovelyJava
Author by

LoveLovelyJava

Updated on July 09, 2022

Comments

  • LoveLovelyJava
    LoveLovelyJava almost 2 years

    I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.

    I use:

    driverChrome.findElements(By.className("blabla"));
    

    to find elements which have "blabla" as their className, for example:

    <span class="blabla" title="the title">...</span>
    

    Now, what if I want to find all elements by their other attributes? something like:

    driverChrome.findElements(By.titleValue("the title"));
    

    This is the code that I am currently using to do this task:

    List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
    
    for (WebElement we : spans) {
    
        if (we.getAttribute("title") != null) {
                if (we.getAttribute("title").equals("the title")) {
                        ...
                        break;
                }
        }
    
    }
    

    but it is not fast and easy to use.

  • JeffC
    JeffC over 8 years
    CSS selectors are the way to go.. and they are faster. CSS Selectors reference
  • LoveLovelyJava
    LoveLovelyJava over 8 years
    That was awesome. WoooooW. Thanks indeed * 1000 :). just one little question, can I do all above with CSS too?
  • LoveLovelyJava
    LoveLovelyJava over 8 years
    and also, in this case: <div class="blabla" title="blabla">Kevin</div> can I get to "Kevin" by xpath
  • J.Lyu
    J.Lyu over 8 years
    Q1 - Basically CssSelector is different with the Xpath selector. If you are familiar with JQuery you will be easy to handle it. Share some primary examples here. e.g - use id <input id="foo" name="bar" /> driver.findElement(By.cssSelector(#foo)); e.g - use class <input id="foo" name="bar" class="jet" /> driver.findElement(By.cssSelector(.jet)); e.g - use id and tag <button id="foo" name="bar"'> (Here assume repeated id) <div id="daddy"> <input id="foo" name="bar" /> </div> driver.findElement(By.cssSelector(input#foo)); Or driver.findElement(By.cssSelector(div#daddy input#foo));
  • J.Lyu
    J.Lyu over 8 years
    If you want to know more about cssSelector you may ask Google... Q2 - Surely you are easy to get text 'Kevin'. Example in java String xPath = "//div[@class='blabla' and @title='blabla']" String text = driver.findElement(By.xpath(xPath)).getText(); //get 'Kevin' Also if you want to get such element by 'Kevin' WebElement e = driver.findElement(By.xpath("//div[text()='Kevin']"))