Home > other >  Complex XPath - Write an XPath in reference to another element
Complex XPath - Write an XPath in reference to another element

Time:03-03

There is a table. This table is a parent div. This parent div has two children. The first child div is the table header and the second one contains the value of headers.

Parent highlighted in bold and children is highlighted in italics bold.

<div > // >>>>>>> Parent div
    <div > // >>>>>>>>>>>> child div - containing table headers
        <div  draggable="false"
             width="1.8050541516245486">
            <div >
                <div></div>
            </div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Ticket</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="true"
             width="10.108303249097473">
            <div >
                <div ><img draggable="false" src="/static/media/drag-indicator.5c17cc6e.svg"
                                                   alt="drag"></div>
                <div>Date</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="true"
             width="8.664259927797833">
            <div >
                <div ><img draggable="false" src="/static/media/drag-indicator.5c17cc6e.svg"
                                                   alt="drag"></div>
                <div>Customer Name</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="true"
             width="7.2202166064981945">
            <div >
                <div ><img draggable="false" src="/static/media/drag-indicator.5c17cc6e.svg"
                                                   alt="drag"></div>
                <div>Supplier</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="true"
             width="7.2202166064981945">
            <div >
                <div ><img draggable="false" src="/static/media/drag-indicator.5c17cc6e.svg"
                                                   alt="drag"></div>
                <div>Material</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Quantity</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="true"
             width="7.2202166064981945">
            <div >
                <div ><img draggable="false" src="/static/media/drag-indicator.5c17cc6e.svg"
                                                   alt="drag"></div>
                <div>Status</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Hauler</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Field Temp</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Gross</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Tare</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Net</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
        <div  draggable="false"
             width="7.2202166064981945">
            <div >
                <div>Max GVW</div>
            </div>
            <div><span ><svg  focusable="false" viewBox="0 0 24 24"
                                                     aria-hidden="true"><path
                    d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"></path></svg></span></div>
        </div>
    </div>
    <div> // >>>>>>>>>>>>>>>> second child div 
        <div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701236" width="7.2202166064981945" >
                    <div>48701236</div>
                </div>
                <div title="2021-01-03T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2021-01-03 10:30:00 am">2021-01-03 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="NH 9.5 75G" width="7.2202166064981945" >
                    <div>NH 9.5 75G</div>
                </div>
                <div title="24.13" width="7.2202166064981945" >
                    <div>24.13</div>
                </div>
                <div title="delivered" width="7.2202166064981945" >
                    <div>delivered</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="90" width="7.2202166064981945" >
                    <div>90</div>
                </div>
                <div title="39.39" width="7.2202166064981945" >
                    <div>39.39</div>
                </div>
                <div title="15.26" width="7.2202166064981945" >
                    <div>15.26</div>
                </div>
                <div title="24.13" width="7.2202166064981945" >
                    <div>24.13</div>
                </div>
                <div title="753962" width="7.2202166064981945" >
                    <div>753962</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701235" width="7.2202166064981945" >
                    <div>48701235</div>
                </div>
                <div title="2021-01-02T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2021-01-02 10:30:00 am">2021-01-02 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="NH 9.5 75G" width="7.2202166064981945" >
                    <div>NH 9.5 75G</div>
                </div>
                <div title="29.36" width="7.2202166064981945" >
                    <div>29.36</div>
                </div>
                <div title="delivered" width="7.2202166064981945" >
                    <div>delivered</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="77" width="7.2202166064981945" >
                    <div>77</div>
                </div>
                <div title="43.19" width="7.2202166064981945" >
                    <div>43.19</div>
                </div>
                <div title="13.83" width="7.2202166064981945" >
                    <div>13.83</div>
                </div>
                <div title="29.36" width="7.2202166064981945" >
                    <div>29.36</div>
                </div>
                <div title="96026" width="7.2202166064981945" >
                    <div>96026</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701234" width="7.2202166064981945" >
                    <div>48701234</div>
                </div>
                <div title="2021-01-01T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2021-01-01 10:30:00 am">2021-01-01 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="NH 9.5 75G" width="7.2202166064981945" >
                    <div>NH 9.5 75G</div>
                </div>
                <div title="14.1" width="7.2202166064981945" >
                    <div>14.1</div>
                </div>
                <div title="delivered" width="7.2202166064981945" >
                    <div>delivered</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="34" width="7.2202166064981945" >
                    <div>34</div>
                </div>
                <div title="26.25" width="7.2202166064981945" >
                    <div>26.25</div>
                </div>
                <div title="12.15" width="7.2202166064981945" >
                    <div>12.15</div>
                </div>
                <div title="14.1" width="7.2202166064981945" >
                    <div>14.1</div>
                </div>
                <div title="22692" width="7.2202166064981945" >
                    <div>22692</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701233" width="7.2202166064981945" >
                    <div>48701233</div>
                </div>
                <div title="2020-12-31T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2020-12-31 10:30:00 am">2020-12-31 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="12.5MM RECYLE LIME" width="7.2202166064981945" >
                    <div>12.5MM RECYLE LIME</div>
                </div>
                <div title="21.44" width="7.2202166064981945" >
                    <div>21.44</div>
                </div>
                <div title="delivered" width="7.2202166064981945" >
                    <div>delivered</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="98" width="7.2202166064981945" >
                    <div>98</div>
                </div>
                <div title="30.27" width="7.2202166064981945" >
                    <div>30.27</div>
                </div>
                <div title="8.83" width="7.2202166064981945" >
                    <div>8.83</div>
                </div>
                <div title="11.44" width="7.2202166064981945" >
                    <div>11.44</div>
                </div>
                <div title="564054" width="7.2202166064981945" >
                    <div>564054</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701232" width="7.2202166064981945" >
                    <div>48701232</div>
                </div>
                <div title="2020-12-30T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2020-12-30 10:30:00 am">2020-12-30 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="NH 9.5 75G" width="7.2202166064981945" >
                    <div>NH 9.5 75G</div>
                </div>
                <div title="29.94" width="7.2202166064981945" >
                    <div>29.94</div>
                </div>
                <div title="rejected" width="7.2202166064981945" >
                    <div>rejected</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="68" width="7.2202166064981945" >
                    <div>68</div>
                </div>
                <div title="38.91" width="7.2202166064981945" >
                    <div>38.91</div>
                </div>
                <div title="8.97" width="7.2202166064981945" >
                    <div>8.97</div>
                </div>
                <div title="29.94" width="7.2202166064981945" >
                    <div>29.94</div>
                </div>
                <div title="741980" width="7.2202166064981945" >
                    <div>741980</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701231" width="7.2202166064981945" >
                    <div>48701231</div>
                </div>
                <div title="2020-12-29T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2020-12-29 10:30:00 am">2020-12-29 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="NH 9.5 75G" width="7.2202166064981945" >
                    <div>NH 9.5 75G</div>
                </div>
                <div title="20.41" width="7.2202166064981945" >
                    <div>20.41</div>
                </div>
                <div title="pending" width="7.2202166064981945" >
                    <div>pending</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="31.99" width="7.2202166064981945" >
                    <div>31.99</div>
                </div>
                <div title="11.58" width="7.2202166064981945" >
                    <div>11.58</div>
                </div>
                <div title="20.41" width="7.2202166064981945" >
                    <div>20.41</div>
                </div>
                <div title="31702" width="7.2202166064981945" >
                    <div>31702</div>
                </div>
            </div>
            <div >
                <div title="" width="1.8050541516245486" >
                    <div ><label><input type="checkbox"></label></div>
                </div>
                <div title="48701230" width="7.2202166064981945" >
                    <div>48701230</div>
                </div>
                <div title="2020-12-28T00:00:00.000-05:00" width="10.108303249097473" >
                    <div title="2020-12-28 10:30:00 am">2020-12-28 10:30:00 am</div>
                </div>
                <div title="Democompany" width="8.664259927797833" >
                    <div>Democompany</div>
                </div>
                <div title="Democompany" width="7.2202166064981945" >
                    <div>Democompany</div>
                </div>
                <div title="12.5MM RECYLE LIME" width="7.2202166064981945" >
                    <div>12.5MM RECYLE LIME</div>
                </div>
                <div title="17.51" width="7.2202166064981945" >
                    <div>17.51</div>
                </div>
                <div title="voided" width="7.2202166064981945" >
                    <div>voided</div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="" width="7.2202166064981945" >
                    <div></div>
                </div>
                <div title="29.99" width="7.2202166064981945" >
                    <div>29.99</div>
                </div>
                <div title="12.48" width="7.2202166064981945" >
                    <div>12.48</div>
                </div>
                <div title="17.51" width="7.2202166064981945" >
                    <div>17.51</div>
                </div>
                <div title="973479" width="7.2202166064981945" >
                    <div>973479</div>
                </div>
            </div>
        </div>
        <div></div>
    </div>
</div>

I want to find out all Gross (10th header column) values. Position Gross is always changing. Per solution I have in mind I need to write an XPath in reference to letter "Gross". So that even if its position changes, XPath will work.

This is the XPath I wrote: //div[text()='Gross']/../../../following-sibling::div//div[@class='sc-cBNfnY fhvIWI'] and it should give 7 cells (elements) but it gives all cells.

How can I write a unique XPath that will give me a list of all 7 Gross values (table cells)?

39.39
43.19
26.25
30.27
38.91
31.99
29.99

CodePudding user response:

This one will do the job in one XPath without using random class-names:

//div[@class[contains(.,'d-flex')]]/div[count(//div[@class[contains(.,'justify-content-between')]][contains(.,'Gross')]/preceding-sibling::*) 1]/div

CodePudding user response:

Since these are actually separated tables you can not do what you want by a single XPath expression.
What you do can is as following:
You can get the column number where Gross text is presented by this XPath:

column = count(//div[./div/div[text()='Gross']]/preceding-sibling::div[contains(@class,'fhvIWI']) 1

With current XML the column is 8 Now you can get the desired cells in the lower table with

//div[@class='sc-cBNfnY fhvIWI'][column]

The following expression

//div[@class='sc-cBNfnY fhvIWI'][8]

Gives the following results:

39.39
43.19
26.25
30.27
38.91
31.99
29.99

  • Related