Locate nested elements using xpath

61 views
Skip to first unread message

tester

unread,
Oct 19, 2017, 12:17:30 PM10/19/17
to webdriver
I am trying to locate a child element which is nested multiple levels. This my DOM :

<md-card-content class = "table1class">
              <h1> Table1Name </h1>
             <ul class = "header">
                   <li>
                         <md-checkbox class=testcheck>
                                <label class=labeltest>
                                       <span>
                                                  <columnname>
                                                                <div> "TestColumnName" </div>
                                                     </columnname>
                                           </span>
                                     </label>
                           </md-checkbox>
                         </li>
                  </ul>
    </md-card>

I am trying to get column name text for a given table name. Can someone please help me with Xpath or css locators to do this?

darrell grainger

unread,
Oct 20, 2017, 3:19:30 PM10/20/17
to webdriver
This is a hard question to answer without the full DOM. There is nothing in here which guarantees a unique XPath or CSS Selector. I can create a few dozen different locators which will work but none of them will be guaranteed to be unique. The more simple the locator the better it will be for maintenance but if it is too simple it will not be unique. Based on what you have posted you could use:

By.cssSelector(".table1class div")
or
By.cssSelector(".testcheck div")

If this is a test or interview question then you would also be expected to answer why these work and when would they fail to work. 

Darrell

Vilson Teixeira Júnior

unread,
Oct 26, 2017, 10:30:40 AM10/26/17
to webd...@googlegroups.com
Try... 

By.xpath("//*[@class='table1class'][@class='labeltest']").getText();

or By.xpath("//*[@class='table1class']//*[@class='labeltest']").getText();


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.



--
Vilson Teixeira Júnior
(51) 98468-3783

darrell grainger

unread,
Oct 27, 2017, 11:52:30 AM10/27/17
to webdriver
Vilson,

The first XPath shouldn't find anything. The "//" at the beginning says search the entire DOM. So the "//*" says to find all elements in the entire DOM. This means it will find <A>, <IMG>, <P>, <TABLE>, etc. Basically, //TABLE will find all table elements, //DIV will find all div elements, //* will find all elements. The bits in the square brackets, e.g. [@class='table1class'] looks for attributes and properties. If the word begins with an at symbol (@) then it is looking for an attribute. So <table class='table1class'> is the element TABLE with the attribute @class='table1class' or in XPath "//table[@class='table1class']". If I have two sets of square brackets, it is for the same element. So //*[@class='table1class'][@class='labeltest'] would only be true for the same element have class='table1class' and having class='labeltest'. For example, <table class='table1class' class='labeltest'> but this should never happen. Instead it would be <table class=table1class labeltest'>.

This is why the first XPath should never happen. It should always return nothing. The second XPath is similar to:

By.cssSelector(".table1class .labeltest")

If you are relying on element attributes, you are probably better to use CSS selectors. They are shorter to write, more flexible that using XPath (e.g. class='table1 test' and class='test table1' are different in XPath but the same in CSS).

Darrell
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.

To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages