2

Im trying to locate an element by xpath using the following path but am receiving the below error. Is it possible for xpath to find this element? Or is this an issue with the driver itself?

I want to be able to differentiate between the values in /p[4]/text()[1] and /p[4]/text()[2]

.//*[@id='content']/p[4]/text()[1]

org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='content']/p[4]/text()[1] is either invalid or does not result in a WebElement. The following error occurred:

InvalidSelectorError: The result of the xpath expression ".//*[@id='content']/p[4]/text()[1]" is: [object XrayWrapper [object Text]]. It should be an element.

This is the html for an example. Im trying to verify the text '(extra questions for session)' with /text().

<div class="ad_field">
<label for="survey_id">Survey</label>
(extra questions for session)
<br>

</div>
adom
  • 129
  • 1
  • 3
  • 9

3 Answers3

5

Well, the problem is that this XPath Expression won't find you an element, but instead you've selected some text.

That is, what the error message also tells you: The given selector [...] does not result in a WebElement.

You've asked for .//*[@id='content']/p[4]/text()[1] which ends in the function /text() which selects not a node, but the text of it. I'm not exactly sure what /text()[1] does but assume it will select the second character in that text. At least I think it's safe to assume that it won't magically convert the text to an DOM Element.

WebDriver however assumes that your expression results in a DOM Element it can represent as a WebElement. This expression does not and thus it throws the expression.

As a hint, you should open the web page and use a tool to manually check your XPath when in doubt. I use Firebug with the FirePath AddOn for this, but you're free to use whatever you find useful.

tilois
  • 682
  • 5
  • 15
  • I am using firebug to get the xpath. which for the text it returns /text()[1] and /text()[2] for each line in the parent /p[4] path. I can verify both lines by only using /p[4] but want to verify each individual line separately. – adom Feb 09 '14 at 21:12
  • Another example of this need is when you have text to the right of a form label, which returns /form/div[5]/text(). I can verify the label and text together by using /form/div[5]. But i want to verify them separately. – adom Feb 09 '14 at 21:13
  • I'm not exactly sure what you're trying to do, but you can't select `/text()` with XPath and WebDriver. WebDriver wants you to select a DOM Element - not a text node. Not every valid XPath expression is valid for a WebDriver to select `WebElement`. – tilois Feb 10 '14 at 13:38
  • 1
    This answer is not always accurate I'm afraid. There is no opposition between "text" and "nodes", since textual content is also a node - a text node. So, `text()` selects the text nodes of the element node in context. Also, `text()[1]` does not select the first character of a text, it selects the first text node of a contextual element node. – Mathias Müller Nov 19 '14 at 10:09
1

I believe the 'text()[1]' part is not acceptable in using xpath to find an element. driver.findElement(By.xpath()) looks for an tag in the webpage. But your xpath .//*[@id='content']/p[4]/text()[1] is not pointing to any tag.

driver.findElement(By.xpath(".//*[@id='content']/p[4]")).getText();

Obtain the text using the above code to a String and do string operations on it to differentiate between the text.

Let me know if this helps you.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • There are actually two lines returned when using .//*[@id='content']/p[4]. Checking in firebug the firepath returns /text()[1] and /test()[2]. Instead of confirming both lines at once, I want to be able to verify them separately. – adom Feb 09 '14 at 21:07
  • Another example of this need is when you have text to the right of a form label, which returns /form/div[5]/text(). I can verify the label and text together by using /form/div[5]. But i want to verify them separately. – adom Feb 09 '14 at 21:09
  • This is the html for an example. Im trying to verify the text '(extra questions for session)' with /text().
    (extra questions for session)
    – adom Feb 10 '14 at 21:46
  • As per my understanding, I don't think it is possible with webdriver. – StrikerVillain Feb 11 '14 at 08:30
0

Use this instead:

//*[@id='content']/p[4][1]

To remove the /text() will make the special xpath work for text node.

Dongdong
  • 2,208
  • 19
  • 28