1

While this info doesn't help resolve my issue, I'll let it be known I'm creating automation tests in Python using Selenium WebDriver.

I have an issue where I want to verify the existence of two identical text elements that appear to have identical XPATH addresses. As you can see from the XPATH of the web page below...

<div class="pagebox">
   <h2>Database</h2>
   Restrict access to group: <input id="database_group" name="database_group" value="postgres" type="text">
   <br></br>
   <h2>Sessions</h2>
    User sessions time out after 
   <input id="session_timeout" name="session_timeout" maxlength="5" value="1200" type="text">
    minutes.
   <br></br>
   <h2>HMI</h2>
   <input id="use_large_header_text" name="use_large_header_text" type="checkbox">
    Use large text in the header bar and link bar on HMI
   <br></br>
   <input id="display_tagging_button" name="display_tagging_button" checked="true" type="checkbox">
    Display tagging button and information in HMI control dialog
   <br></br>
   <br></br>
   <h2>Inactive Redirect</h2>
    Auto redirect after user is inactive for 
   <input id="inactive_time" name="inactive_time" maxlength="4" placeholder="Min: 0 (Default) Max: 1440" size="25" value="0" type="text"></input>
    minutes. 
   <span id="ieInactTime" style="font-style: italic"> Min: 0 (Default) Max: 1440</span>
   <br></br>
    Redirect Address: 
   <input id="inactive_page" name="inactive_page" placeholder="File Path e.g. /Home/" size="30" value="" type="text"><span id="ieInactPage" style="font-style: italic"> File Path e.g. /Home/</span>
   <br></br>

...the phrase "minutes." shows up twice [line 8 and line 21], but it doesn't show up in a way where I can use the element's XPATH to locate which element I want to verify the existence of as I normally would using methods you'd find here, here, or here. Both text elements appear to have the same exact XPATH address:

//div[@class='pagebox']

I tried to distinguish between the two text elements using brackets to signify which text elements I'd like to verify...

e.g. //div[@class='pagebox']/[1] & //div[@class='pagebox']/[2]

...but that didn't work.

The closest I could find to distinguishing between the two of these text elements was to use the 'text()' feature of XPATH.

//div[@class='pagebox']/text()[contains(.,'minutes')][1]

//div[@class='pagebox']/text()[contains(.,'minutes')][2]

Is there any way to verify these two text elements using XPATH?

Community
  • 1
  • 1
rwbyrd
  • 416
  • 5
  • 24
  • You could rename each of the "minutes" strings to minutes_X in your inspector. Should be easier to find the one you are looking for. – Kevin S Feb 25 '16 at 18:22
  • 1
    In your sample code `//div[@class='pagebox']/input[2]` is "Use large text in the header bar and link bar on HMI". Using `input[n]` is how you distinguish them. What is it that you are asking? – SiKing Feb 25 '16 at 18:39
  • @KevinS Renaming the "minutes." strings, unfortunately, isn't an option. This is the scenario I'm stuck with & have to find a way to make it work. – rwbyrd Feb 25 '16 at 18:45
  • @SiKing My sample code was just an example, not to be taken literally. My sample code is nothing more than an example of how someone might distinguish between two identical strings normally, however, that situation doesn't fit my case. I'll change that example to remove any confusion. – rwbyrd Feb 25 '16 at 18:47
  • @rwbyrd Renaming from your browsers inspector is only temporary and would allow you to identify which element you are looking for. Once you know which element you specifically want you can grab the xpath. – Kevin S Feb 25 '16 at 18:58
  • 1
    Still unclear what you are asking! Can you describe in plain English how you would like to tell them apart? – SiKing Feb 25 '16 at 19:04
  • @KevinS Renaming from the browser inspector would work. The problem with using that method is this is a scenario that will be encountered through automation testing. So this scenario will be happening on Chrome, Firefox, IE, & Edge across multiple Linux and Windows platforms. I won't always be able to have access to the browser on the other machines to use the browser inspector to relable the text. Does that make sense? – rwbyrd Feb 25 '16 at 19:25
  • @SiKing, As you can see above, the phrase I want to search for, "minutes." shows up in two locations. I could do a search in automation using the XPATH "//div[@class='pagebox'] and it would find this phrase. The issue is that the phrase shows up twice with the exact same XPATH code. I don't know how to distinguish which one of those two identical phrases I'm looking for and that is what I need help with. – rwbyrd Feb 25 '16 at 19:30
  • How, in plain English, would you distinguish which one of those two identical phrases you're looking for? – Dabbler Feb 26 '16 at 09:01

2 Answers2

0

You can use JavaScriptExecutor to find text node inside the element div. See C# code below:

IWebElement e = driver.FindElement(By.ClassName("pagebox"));
string script = "var nodes = arguments[0].childNodes;" +
    "var text = [];" +
    "var count=0;" +
    "for (var i = 0; i < nodes.length; i++) {" +
    "    if ((nodes[i].nodeType == Node.TEXT_NODE) && (nodes[i].textContent.trim()=='minutes.')) {" +                
    "        text[count]= nodes[i].textContent.trim();" +
    "        count++;" +
    "    }" +
    "}" +
    "return text;";
Object obj = ((IJavaScriptExecutor)driver).ExecuteScript(script,e);
Buaban
  • 5,029
  • 1
  • 17
  • 33
0

To distinguish between the two different text elements "minutes.", you need the following XPATH:

For the 1st "minutes.": //div[@class='pagebox'][text()[contains(.,'minutes')][1]]

For the 2nd "minutes.": //div[@class='pagebox'][text()[contains(.,'minutes')][2]]

rwbyrd
  • 416
  • 5
  • 24