This answer leads me to believe that driver.find_elements_by_xpath("//a[@href]")
and driver.find_elements_by_tag("a")
do the same thing. If they are the same, why have both methods?
Asked
Active
Viewed 111 times
1

financial_physician
- 1,672
- 1
- 14
- 34
-
They are not the same, they serve different purpose as some users don't understand how xpath works, they may wish to use the simpler method. – metatoaster Nov 20 '20 at 04:01
-
1The cited answer claims that the xpath command should find all the `a` elements that have `href`s on them. And I think the second one should do the same thing. Is the only different that the former example explicitly requires it to have a `href`? – financial_physician Nov 20 '20 at 04:04
-
1The specific provided `xpath` will indeed return all `a` elements that have `href`, while simply `.find_elements_by_tag("a")` will return all `a` elements regardless of other attributes (which will include `a` elements without `href`). – metatoaster Nov 20 '20 at 04:07
-
1@financial_physician Your interpretation within your comment was correct. However you jumped the gun too early to accept an open ended answer :) – undetected Selenium Nov 20 '20 at 08:54
2 Answers
1
driver.find_elements_by_xpath
can have a whole path, with only specific patterns in places to be considered, while driver.find_elements_by_tag
will only find the "a"
tags.

Red
- 26,798
- 7
- 36
- 58
1
driver.find_elements_by_xpath will search the whole webpage with irrespective of any tags whether it is class, name, id etc. While driver.find_elements_by_tag() will only search those webelements which will have particular tag. Hence find_elements_by_tag will be faster than find_elements_by_xpath.

Himanshu Varshney
- 11
- 2
-
Wouldn't the driver have to look at the elements to know what tag they have regardless of the method? I'm not sure how `find_elements_by_xpath` would not know something that `find_elements_by_tag` already knows – financial_physician Nov 20 '20 at 19:20
-
1Yes, you are correct. But, if we are talking about which one is faster than other, then if we are having the exact tag at first then it would be quicker to find. – Himanshu Varshney Dec 09 '20 at 07:04