What is the difference between dot and text in XPath

0 votes

I'm confused between using dot and text() in XPath. For example, the following find_element lines returns same element:

driver.get('http://stackoverflow.com/')
driver.find_element_by_xpath('//a[text()="Ask Question"]')

and

driver.get('http://stackoverflow.com/')
driver.find_element_by_xpath('//a[.="Ask Question"]')

Then what exactly is even the difference between the two? Benefits/ Drawbacks with using '.' and 'text()'?

Apr 25, 2018 in Selenium by kappa3010
• 2,090 points
8,268 views

1 answer to this question.

0 votes

Even though there is some difference between '.' and 'text()', in the example you have specified, it cannot be explained. 

Consider the below html code. Here, there is no differnce between using the two.

<html>
  <a>Ask Question</a>
</html>

('//a[.="Ask Question"]') and ('//a[text()="Ask Question"]') will return exactly the same result.

But then consider this html code. There will be a difference in this case.

<html>
  <a>Ask Question<other/>
  </a>
</html>

Since there is a child element <other> following "Ask Question", (//a[text()="Ask Question"]) will still return the <a> element, but (//a[.="Ask Question"]) will not return anything.

That's because the meaning of the two predicates (everything between [ and ]) is different. [text()="Ask Question"] will return true if any text node in that element contain exactly "Ask Question". However, [.="Ask Question"] will return true if the string value of an element is identical to "Ask Question".

answered Apr 25, 2018 by king_kenny
• 3,710 points

Related Questions In Selenium

0 votes
2 answers

What is the difference between / and // in xpath?

/ - Selects from the root node // ...READ MORE

answered May 29, 2019 in Selenium by anonymous
28,191 views
0 votes
2 answers
0 votes
3 answers

What is the difference between relative and absolute XPath?

Absolute Xpath: It contains the complete path ...READ MORE

answered Dec 20, 2018 in Selenium by Nabarupa
67,157 views
+1 vote
2 answers

What is the difference between findElement and findElements in Selenium Webdriver?

FindElement Command -This method locates for the ...READ MORE

answered Feb 11, 2020 in Selenium by anonymous
8,341 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,567 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,084 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,178 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,840 views
0 votes
1 answer

What is the difference between thoughtworks.selenium and openqa.selenium selenium?

thoughtworks.selenium is the original Selenium (aka Selenium 1, ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,710 points
2,962 views
0 votes
1 answer

What is difference the between Selenium and Celerity?

If you are talking about speed, use Celerity. If ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,710 points
991 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP