user13725155
user13725155

Reputation:

Selenium selector: Issue when using text() in xpath

I have html form which has below template:

<div class="order__details">
                    <p><span class="label">Order Status:</span>In Progress</p>
                </div>

I want to get the XPath for "In Progress" Text; the text is dynamic

I tried: //div[@class='order__details']/p/text()

But it gives me error:

OpenQA.Selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//div[@class='order__details']/p/text()" is: [object Text]. It should be an element

Upvotes: 0

Views: 803

Answers (2)

E.Wiest
E.Wiest

Reputation: 5915

You can use one of the following XPath expressions :

//span[@class='label'][contains(.,'Order Status:')]/following::text()[1][normalize-space()]

//div[@class="order__details"]/p/text()[normalize-space()]

Output : In Progress

EDIT : For Selenium, get the text with :

driver.FindElement(By.Xpath("//div[@class='order__details'][./p[contains(.,'Order Status:')]]/p")).Text;

Upvotes: 0

lab9
lab9

Reputation: 586

The text that you search is not within your span tag, that's why your xpath fails. This xpath expression should serve your needs:

"//div[@class='order__details']/p/text()"

Upvotes: 0

Related Questions