My question is about specifics of using dot and text()
in XPath
. For example, following find_element
lines returns same element:
driver.get('http://stackoverflow.com/') driver.find_element_by_xpath('//a[text()="Ask Question"]') driver.find_element_by_xpath('//a[.="Ask Question"]')
So what is the difference? What are the benefits and drawbacks of using .
and text()
?
The XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.
Using XPath- text() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("//*[text()='Google offered in')]"));
As explained in this post, we've used dot (.) operator to access instance variables of a class by creating object to that class. In the same way we can access methods of the class by creating an object to it.
Difference between “/” and “//” in XPathSingle slash is used to create absolute XPath whereas Double slash is used to create relative XPath. 2. Single slash selects an element from the root node. For example, /html will select the root HTML element.
There is a difference between .
and text()
, but this difference might not surface because of your input document.
If your input document looked like (the simplest document one can imagine given your XPath expressions)
Example 1
<html> <a>Ask Question</a> </html>
Then //a[text()="Ask Question"]
and //a[.="Ask Question"]
indeed return exactly the same result. But consider a different input document that looks like
Example 2
<html> <a>Ask Question<other/> </a> </html>
where the a
element also has a child element other
that follows immediately after "Ask Question". Given this second input document, //a[text()="Ask Question"]
still returns the a
element, while //a[.="Ask Question"]
does not return anything!
This is because the meaning of the two predicates (everything between [
and ]
) is different. [text()="Ask Question"]
actually means: return true if any of the text nodes of an element contains exactly the text "Ask Question". On the other hand, [.="Ask Question"]
means: return true if the string value of an element is identical to "Ask Question".
In the XPath model, text inside XML elements can be partitioned into a number of text nodes if other elements interfere with the text, as in Example 2 above. There, the other
element is between "Ask Question" and a newline character that also counts as text content.
To make an even clearer example, consider as an input document:
Example 3
<a>Ask Question<other/>more text</a>
Here, the a
element actually contains two text nodes, "Ask Question" and "more text", since both are direct children of a
. You can test this by running //a/text()
on this document, which will return (individual results separated by ----
):
Ask Question ----------------------- more text
So, in such a scenario, text()
returns a set of individual nodes, while .
in a predicate evaluates to the string concatenation of all text nodes. Again, you can test this claim with the path expression //a[.='Ask Questionmore text']
which will successfully return the a
element.
Finally, keep in mind that some XPath functions can only take one single string as an input. As LarsH has pointed out in the comments, if such an XPath function (e.g. contains()
) is given a sequence of nodes, it will only process the first node and silently ignore the rest.
There is big difference between dot (".")
and text()
:-
The dot (".")
in XPath
is called the "context item expression" because it refers to the context item. This could be match with a node (such as an element
, attribute
, or text node
) or an atomic value (such as a string
, number
, or boolean
). While text()
refers to match only element text
which is in string
form.
The dot (".")
notation is the current node in the DOM. This is going to be an object of type Node while Using the XPath
function text() to get the text for an element only gets the text up to the first inner element. If the text you are looking for is after the inner element you must use the current node to search for the string and not the XPath
text() function.
For an example :-
<a href="something.html"> <img src="filename.gif"> link </a>
Here if you want to find anchor a
element by using text link, you need to use dot (".")
. Because if you use //a[contains(.,'link')]
it finds the anchor a
element but if you use //a[contains(text(),'link')]
the text()
function does not seem to find it.
Hope it will help you..:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With