0
votes

I have a web page with a left hand menu. It is made up of many div tags. I have noticed when my Selenium Python script runs it is not clicking the text I want clicked from the left hand menu. It is clicking something else. My Xpath is not correct.

I would like to locate the text "Statistics" (it is in a div\span tag) which has the parent div text "Analysis"

It is not clicking the correct text "Statistics" because there maybe another "Statistics" somewhere in the HTML source. If i start from the div tag which has the text "Analysis" and then find the text "Statistics" then I will get the correct element.

My Xpath is:

.//div//span[@title="Analysis"]/following::div[5]//span[text()="Statistics"]

The HTML is:

    <div>
    <span class="" title="Analysis"
          style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Analysis</span>
</div>
</div>
</div>
</div>
</div>
<div style="overflow: hidden;">
    <div>
        <div>
            <div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-expanded="false"
                 aria-level="2">
                <div class="GJPPK2LBIF" style="padding-left: 16px;">
                    <div class="GJPPK2LBIF GJPPK2LBKF" style="padding-left: 16px;position:relative;" onclick="">
                        <div class="GJPPK2LBJF" style="left: 0px;width: 15px;height: 15px;position:absolute;">
                            <img border="0"
                                 style="width:15px;height:15px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA7/HbnjJn53wAAAABJRU5ErkJggg==) no-repeat 0px 0px;"
                                 src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
                                 onload="this.__gwtLastUnhandledEvent=" load";"/>
                        </div>
                        <div class="GJPPK2LBLF">
                            <div style="padding-left: 22px;position:relative;zoom:1;">
                                <div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;">
                                    <img border="0"
                                         style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaekJggg==) no-repeat 0px 0px;"
                                         src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
                                         onload="this.__gwtLastUnhandledEvent=" load";"/>
                                </div>
                                <div>
                                    <span class="" title="Statistics"
                                          style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Statistics</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

Thanks, Riaz

1
I'm confused, is it working or not? - Guy
This doesn't answer your question, however I would consider your xpath - its quite rigid and because of this it will increase the liklihood of this test failing over time, just because you played around with some divs. - lukeaus
Consider xpath helper for Chrome - chrome.google.com/webstore/detail/xpath-helper/… - using this you can see what matches and also copy and paste the xpath for existing elements - lukeaus
hard to say without seeing the whole page, but xpath looks OK. Maybe you don't need a dot at the start of the xpath (i.e. //div//span[@title="Analysis"]/following::div[5]//span[text()="Statistics"]), but otherwise I don't see why this doesn't work from the fragment you posted. - Kiril S.
Thanks for the guidance. Yes it's strange it does not seem to work. There is a button on the page also and it is trying to click the button instead of Statistics. I have inspected the button and it has an ID locator. It is button id="analysis_statistics_pb_edit" - Riaz Ladhani

1 Answers

1
votes

If you have FireFox with FirePath you can test the xpath and see how many and which matches you get. For instance:

//span[text()="Statistics"]

This may result in 1 matching node but also in more. Let's assume there's two matches and the one you want is the second one. Then you'd choose:

//span[text()="Statistics"][2]