Reputation: 971
I need to access the first cell of something that looks like a table (but is slightly weird - the DIV for the first column is structurally "above" the rest of the cells on the same row).
<div class="slick-viewport slick-viewport-top slick-viewport-left" tabindex="0" hidefocus="" style="overflow: auto; width: 100%; height: 513px;">
<div class="grid-canvas grid-canvas-top grid-canvas-left" tabindex="0" hidefocus="" style="height: 825px; width: 1336px;" unselectable="on">
<div class="ui-widget-content slick-row even lastClicked selected" role="row" row="0" style="top:0px;">
<div class="slick-cell l0 r0 uppercase selected" aria-describedby="inforDataGrid160360C1" tabindex="-1" role="gridcell"><span style="float: left;display:inline-block;height:1px;width:0px"></span><button type="button" class="tree-expand inforIconButton closed"><span></span></button><span>ABC</span></div>
<div class="slick-cell l1 r1 uppercase selected" aria-describedby="inforDataGrid160360C2" tabindex="-1" role="gridcell">DEF</div>
<div class="slick-cell l2 r2 uppercase selected" aria-describedby="inforDataGrid160360C3" tabindex="-1" role="gridcell">GHI</div>
</div>
<div class="ui-widget-content slick-row odd" role="row" row="1" style="top:25px;">
<div class="slick-cell l0 r0 uppercase" aria-describedby="inforDataGrid160360C1" tabindex="-1" role="gridcell"><span style="float: left;display:inline-block;height:1px;width:0px"></span><button type="button" class="tree-expand inforIconButton closed"><span></span></button><span>JKL</span></div>
I am looking for something useful that points out ABC. As one can see the div above that has the property row="0"
which seems reasonable to use. The div with ABC in has the class l0 r0
which also seems useful (but what is slightly counter intuitive is that the class of the next cell on the same line - containing DEF - is not l0 r1
or l1 r0
or something like that but instead l1 r1
).
Also, many DIV levels up there is an id="contentBody"
that I would like to include.
So something like //contentBody/*/row="0"/class="l0 r0"
.
How do I construct this XPath?
(DEF is the second cell/column on the first line, GHI is the third cell/column on the first line. JKL is the first cell/column on the second line. I included this for you to get an idea of how this table is constructed).
Upvotes: 1
Views: 544
Reputation: 52665
This one should return required div
:
//div[@id="contentBody"]//div[@row="0"]/div[contains(@class, "l0") and contains(@class, "r0")]
The same in CSS-selector:
div#contentBody div[row="0"]>div.l0.r0
You can use selectors cheat-sheet for creating XPath/CSS selectors as well as tutorial, documentation for deep understanding
Upvotes: 6