Issue
Why should I use functions to find elements, like find_elements_by_id
or find_elements_by_class
, when I can easily do that with find_elements_by_xpath
?
(I know that in Selenium 4.0 we don't use these functions anymore, instead we use find_element(By.CLASS_NAME, "class"))
but still, can someone explain it to me, I would very appreciate any advice)
Solution
The "Class name" and "ID" of the elements don't often change to a different name. If you search for elements with xpath, it can change for example with every new section added to the page.
Example before a new section gets added:
find_element(By.CLASS_NAME, "class"))
find_element(By.ID, "id"))
find_element(By.XPATH, "div/div"))
Example after a new section gets added:
find_element(By.CLASS_NAME, "class"))
find_element(By.ID, "id"))
find_element(By.XPATH, "div/div/div/a"))
Result: If you were finding elements with xpath, you'll have to change all the xpaths now. The class name and Id stayed the same.
A way more effective way is if you write your own xpath.
Example:
//a[contains(@class,'something')]
How? -> https://www.guru99.com/xpath-selenium.html
Answered By - Tadej Blatnik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.