0

I am able to inspect an element using browser's Developer tools like below:

Inspected the element using FireFox:

/html/body/div[1]/div/div[8]/form/div[2]/spring:eval/table/tbody/tr[2]/td[5]

Inspected the same element using Chrome as below:

//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]

However, when I tried to use any of these XPaths in the script, then the execution fails with below stack traces:

WebElement formElement = wd.findElement(By.xpath("//*[@id=\"searchVO\"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));

Or

WebElement formElement = wd.findElement(By.xpath("/html/body/div[1]/div/div[8]/form/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));

Stack Trace:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5] because of the following error:
NamespaceError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]' contains unresolvable namespaces.
…
…
…
*** Element info: {Using=xpath, value=//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)

I have also confirmed that there is no Frames on the webpage; however, the above element is present within a Form – snippet of this element is given below – I am trying to find below status ‘Operative’ or ‘Stopped’ - above Xpath belongs to this status field.

On following to this search, if the status is ‘Operative’ then I need to click on the Product1.

<form id="searchVO" action="/webpage/webpage/search-products?execution=e2s1" method="post">
<div id="nc-bd">
<h2>
Search results for 
Some phone number
</h2>
</div>
<br>
<div>
<spring:eval expression="@webpageServicesProperties['SOMEVARIABLE_SWITCH']" var="someVariableSwitch">
<div id="errorMessage" style="display: none;">
<label class="error-message" id="errorBar"></label>
</div>
<table style="width: 100%;">
    <caption>Products   </caption>
    <tbody>
        <tr>
            <th>Name</th>
            <th>Product</th>
            <th>Some phone number</th>
            <th>Username</th>
            <th>Status</th>
        </tr>
                        <tr>
                            <td>Mr XXX</td>
                                    <td>
                                        <a href="CustomerID=333333333">Product1</a>
                                    </td>
                            <td>
                            09876543210
                            </td>
                                <td class="wrap-txt">someemail@ID.com</td>
                            <td>Operative</td>
                       </tr>
                        <tr>
                            <td>Mr XXX</td>
                                    <td>
                                        <a href="CustomerID=4444444444">Product2</a>
                                    </td>
                            <td>
                            09876543210
                            </td>
                                <td class="wrap-txt">someemail@ID.com</td>
                            <td>Stopped</td>
                       </tr>
    </tbody>
</table>
    </spring:eval></div>
</form>

Any help on finding the cause of this issue, please.

PraveenKS
  • 1,145
  • 3
  • 13
  • 28
  • Not sure I've had to deal with namespaces in xpath before... Out of curiosity, does this work? `WebElement formElement = wd.findElement(By.xpath("collection()//*[@id='searchVO']/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));` I can provide a kind of explanation if so, but I don't have a way to test it. – mrfreester May 15 '19 at 16:24
  • @mrfreester, thank you for your response, I have used your same Xpath and got below exception:; did I missed any? org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression collection()//*[@id='searchVO']/div[2]/spring:eval/table/tbody/tr[2]/td[5] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'collection()//*[@id='searchVO']/div[2]/spring:eval/table/tbody/tr[2]/td[5]' is not a valid XPath expression. – PraveenKS May 16 '19 at 07:31
  • It was a stretch at trying to get your xpath to accept all namespaces. I've never done it before, so not surprising that it didn't work :) Alternate selectors work great! I'm sure there is a way to include the namespace you need to get the `spring:eval` part to work. I'm guessing that's what's causing your issue. – mrfreester May 17 '19 at 21:18

2 Answers2

1

Here is the xpath for operative.

WebElement formElement = wd.findElement(By.xpath("//div[@id='nc-bd']/following-sibling::div[1]//table//tr[2]//td[5]"));

And xpath for stopped.

WebElement formElement = wd.findElement(By.xpath("//div[@id='nc-bd']/following-sibling::div[1]//table//tr[3]//td[5]"));
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • many thanks Kunduk, your above solution works perfect. +1. Can you confirm whether [following-sibling] will also work even if the child nodes are present under iFrame? – PraveenKS May 15 '19 at 17:49
  • Yes why not but you have to switch to iframe first – KunduK May 15 '19 at 18:58
0

Here is the xpath that will identifies all the products with Operative.

//form[@id='searchVO']//table//tr[td[normalize-space(.)='Operative']]/td[count(./ancestor::table//tr[1]/th[normalize-space(.)='Product'])+1]
supputuri
  • 13,644
  • 2
  • 21
  • 39