Hi every body,
I want to get xpath of “Order Data Entry” text but can’t
I tried contains() but still not work
Help me pls.
//*[contains(text(), 'Order Data Entry')]
or
//*[normalize-space()='Order Data Entry']
Well, you are asking a naive question.
//span[contains(text(), 'Order Data Entry')]
This XPath expression returns a reference to HTML element <span>
, of which text content contains “Order Data Entry”. Please note, this returns an element, not a text (java.lang.String).
As far as XPath syntax is concerned, the following XPath expression returns a text:
//*[contains(text(), 'Order Data Entry')]/text()
Howerver this XPath exression, which has /text()
at the tail, is useless in the WebDriver world. Katalon’s WebUI.findElementPresent(..)
etc depends on the WebDriver’s driver.findWebElement(By.xxx)
.
WebElement element = driver.findWebElement(By.xpath("//*[contains(text(), 'Order Data Entry')]"))
this would work, the element
will point a <span>
. But
WebElement element = driver.findWebElement(By.xpath("//*[contains(text(), 'Order Data Entry')]/text()"))
this would NOT work, as a java.lang.String object can not be cast to WebElement.
You can NOT select an inner text of any Element at all using WebUI.verifyElementPresent(TestObject)
.
But you can do:
String text = WebUI.getText(findTestObject("name of TestOject with Path //*[contains(text(), 'Order Data Entry')]"))
The resulting text
variable will have a String value same as the original <span>
element has:
" Order Data Entry
"
WebUI.getText()
method internally selects the <span>
element, evaluates it to a text() value, and returns the text() result.
Do you find it’s too complicated?
Yes, it is. Let me say again, you are asking a naive question.