James
James

Reputation: 607

Internet Explorer xpath not finding element

I am trying to find an element located at

xpath = //html[1]/body[1]/div[1]/div[2]/div[1]/div[2]/p[1] 

In Firefox this works, however in Internet Explorer it doesn't. IE can find the element but it is at

//html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/p[1] 

I guess this could make sense given they probably format some source differently, my issue is that if I look at the source on the IE page, then to me the first one that works in FF looks correct. If I put the source outputted from IE into FF then Firefox finds the element correctly at the location I am expecting.

I have the page source which I got from IE which can be found at http://pastebin.com/Vg9J7cxn. Also included is the source from Firefox

Does anyone know why IE is unable to find the element? I am finding the xpath by iterating back through the dom structure, so does anyone know a workaround I can use to find elements at these "IE" xpaths in the future?

Upvotes: 0

Views: 1459

Answers (2)

Wayne
Wayne

Reputation: 60414

This is what you get when you strip the content of the source down to just the structural elements:

<html>
    <head></head>
    <body>
        <div class='appOuterWrapper'>
            <div class='appHeaderContainer'>
                <div class='appHeader'>
                    <div class='appTop'>
                        <div class="appLogonOff"></div>
                    </div>
                    <div class="appCmsMenu">
                        <ul id="nav" class="top"></ul>
                        <div class="breadcrumbs">
                            <div class="crumbs"></div>
                            <div class="thedate"/>
                            <span/>
                        </div>
                        <script></script>
                    </div>
                </div>
            </div>
        </div>
        <div class="containerWrapper cmsContent">
            <div class="appWholePage appContent">
                <div class="home-sidebar"></div>
                <div class="page-body home-page-content"></div>
                <div class="containment"></div>
            </div>
        </div>
    </div><!-- <==== THIS IS INVALID -->
        <div class='appFooterContainer'>
            <div class='appFooter'>
                <span class='links'></span>
                <span class='copyright'></span>
            </div>
            <div class="TestAppitLink"></div>
        </div>
    </body>
</html>

This is the same in both versions of the linked source. Notice my comment, which is pointing out an extra ending div tag:

</div><!-- <==== THIS IS INVALID -->

If you look at how my parser formatted the document (based on the structure above), you'll see that there is no element at:

/html[1]/body[1]/div[1]/div[2]

...because

/html[1]/body[1]/div[1]

...only contains one div. (In other words, it agrees with IE.)

My guess is that Firefox and IE are treating this input tree differently, in an effort to present it to the XPath engine as a well-formed document.

Options:

  • Fix the source document
  • Approach the expression using classes, instead of position. You may find one that matches the tree in both browsers. For example:

    /html/body/div[@class='appOuterWrapper']/<rest_of_expression>
    

Upvotes: 3

kwikness
kwikness

Reputation: 1445

Inside the first div (//html[1]/body[1]/div[1]), there is only one div child. I don't see how you could address a 2nd div.

What's the class of the div you're trying to get to?

Upvotes: 0

Related Questions