0
<span id="UniversalRepositoryExplorer_treeNode7_name" style="white-space:nowrap;"> == $0
 <img id="UniversalRepositoryExplorer_treeNodeIcon_7" src="../../images/server_running.gif" 
 style="width:16px;height:16px;" alt   border="0"> == $0
 "&nbsp;Running&nbsp;" == $0

tag span has inside tag img and below text Running doesn't have any tag name

I have tried the below x-path that didn't work:

//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']

Can someone suggest to me how to get Running through x-path?

JeffC
  • 22,180
  • 5
  • 32
  • 55
itsabhishk
  • 11
  • 1
  • Are you looking to locate `Running` text node ? – cruisepandey Oct 26 '21 at 16:26
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 26 '21 at 16:35
  • 2
    It's very hard to ask questions that people will understand unless you learn the right terminology. You're confused about the XML data model: element nodes, element names, text nodes, etc. You need to do some reading. – Michael Kay Oct 26 '21 at 17:06
  • You need to post the actual HTML from the page. Right now the HTML you've posted is not valid... you've pasted several " == $0" which is not HTML and your tags aren't closed properly. – JeffC Oct 26 '21 at 18:35
  • You need to add a tag for the language you are using along with your current code, what about the current code isn't working, any error messages, etc. – JeffC Oct 26 '21 at 18:37
  • I just gave you the reference to find xpath. I cannot copy paste whole html part as it is not allowed. – itsabhishk Oct 27 '21 at 07:44
  • Whatever I saw upon inspecting element. I wrote that part as is. – itsabhishk Oct 27 '21 at 07:49
  • My main doubt is - if I see text in a web page and that text doesn't have any tags nor opening nor closing. So how would I get that to my JAVA code. – itsabhishk Oct 27 '21 at 07:51
  • The text you want IS inside a tag... but the HTML you have posted is not valid. You don't have to post all the HTML, just a snippet but it needs to be valid so we can build locators. – JeffC Oct 27 '21 at 14:20

4 Answers4

0

Basically, you're trying to get the text node.

xpath: '//span/text()[last()]'

Example:

document.evaluate("//span/text()[last()]", window.document, null, XPathResult.ANY_TYPE, null).iterateNext()
 
// output: " Running "
Faizan AlHassan
  • 389
  • 4
  • 8
  • `text()` doesn't work with Selenium. – JeffC Oct 26 '21 at 18:33
  • Dear, You can execute script easily in selenium. Use: `driver.execute_script("""document.evaluate("//span/text()[last()]", window.document, null, XPathResult.ANY_TYPE, null).iterateNext()""")` – Faizan AlHassan Oct 26 '21 at 19:06
  • ...and that's going to return 100+ SPAN elements on the page that contain text. How does that target the element OP wants? – JeffC Oct 26 '21 at 20:44
  • you just need to update the XPath, I created a sample XPath. You can update it till span element after reviewing the exact targetted HTML like `//span[@id='UniversalRepositoryExplorer_treeNode7_name']/text()[last()]` – Faizan AlHassan Oct 27 '21 at 07:55
  • His question was not how to get span, but how to get text node. So I addressed only that part. – Faizan AlHassan Oct 27 '21 at 10:32
  • But you have to get the *right* SPAN to get the right text node which your posted answer does not do. You don't have to do all this JS, you can just use simple Java. – JeffC Oct 27 '21 at 14:18
  • Dear, my answer is addressing the question "how to get text node?" (which he referred to as the element without tag name), I didn't provide solution for "how to get span?" Also executing JS is a lot faster. In fact, if you have worked with puppeteer, you know it mostly works by injecting JS. Also using the JS technique makes you capable of using multiple browsers controlling tools because all of them allow injecting JS. – Faizan AlHassan Oct 28 '21 at 06:14
  • I don't understand what you don't understand... OP asked how to get a specific text node and provided the HTML. You are acting like he gave no specifics whatsoever and wants to know how to get any text node. That was not the question. Go read it again. Running JS from inside Java is not faster. If you want to write all your code in JS, then just use JS as your base language and don't bother with Java. – JeffC Oct 28 '21 at 14:02
  • Your answers showing your lack of knowledge. You are working with browsers. And selenium provided executing JavaScript, you think it's without any purpose? – Faizan AlHassan Oct 30 '21 at 01:31
  • Hmm... my answer works, your answer doesn't. I've already explained why. You can either learn from your mistake here or not. I've been employed fulltime to do automation for 25 years, 15 of those years were web UI automation, and 7 of those years were Selenium automation in multiple languages. There is one Selenium method to run JS and about 50 to do everything else in the core language, in this case Java. The JS execute method is a "just in case" method but shouldn't be needed 99.9% of the time if you do things correctly. – JeffC Oct 30 '21 at 03:22
  • I saw your answer, you're calling `getText`, which will combine all the text nodes in that span. So my answer can be used to find exact text node. So its your choice, do what solve the problem – Faizan AlHassan Oct 30 '21 at 07:51
0

Looking at the element you have noted above, if I was trying to evaluate for a cucumber step, I would write this for selenium-java:

@And("^I can view the \"([^\"]*)\" image$")
    public void iCanViewTheImage(String text) throws Throwable {
        String imgXpath = "//span[contains(text(),'" + text + "') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']"
        driver.findElement(By.xpath(imgXpath));
        assertTrue(driver.findElement(By.xpath(imgXpath)).getText().contains(text));

}

where the regex "([^"]*)" would be "Running" in your cucumber step: And I can view the "Running" image

Your xpath with the text based on the above will be:

//span[contains(text(),'Running') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']

or simply

//span[contains(text(),'Running') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]
djmonki
  • 3,020
  • 7
  • 18
0

The problem with your locator is that an IMG tag is self-closing (there is no </img> tag) so it can't contain text. The parent SPAN is likely the element that contains the "Running" text.

You haven't posted valid HTML for the portion being discussed so the below HTML is what I'm guessing the general structure looks like.

<span id="UniversalRepositoryExplorer_treeNode7_name" style="white-space:nowrap;">
    <img id="UniversalRepositoryExplorer_treeNodeIcon_7" src="../../images/server_running.gif" style="width:16px;height:16px;" alt   border="0">
    "&nbsp;Running&nbsp;"
</span>

Given that HTML, the code would look like

driver.findElement(By.id("UniversalRepositoryExplorer_treeNode7_name")).getText();

When you grab the SPAN and then use .getText(), it will get all the text contained within that SPAN. This is of course a guess since you haven't posted all the HTML under that span. This may get more than what you wanted but that's the best we can do with an incomplete question. If you add more details, we can adjust our answers.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

The text Running is within a text node and it is the second child of it's parent <span> tag. So to retrieve the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[2].textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span#UniversalRepositoryExplorer_treeNode7_name")))).toString());
    
  • xpath:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[2].textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='UniversalRepositoryExplorer_treeNode7_name']")))).toString());
    

References

You can find a couple od relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352