Why use find_element(By...) instead of find_element_by_
Asked Answered
A

4

14

What's the purpose and upside of using By from selenium.webdriver.common.by instead of instead of the normal find_element_by_... methods? For example:

driver.find_element_by_id('some_ID')

vs:

from selenium.webdriver.common.by import By
driver.find_element(By.ID, 'some_ID')
Atlantic answered 11/4, 2017 at 20:18 Comment(0)
F
23

According to documentation find_element() seem to be kind of "private" method that is used by find_element_by_...() methods and also might be used in Page Object

So using Page Object pattern is the reason why you might need find_element() + By instead of find_element_by_...().

For example, you have some variable that contains elements' id value

link_id = "some_id"

and you use it to locate element as

my_link = driver.find_element_by_id(link_id)

If for some reason id attribute was removed from element, you need both to update selector and replace find_element_by_...() method in my_link as

link_class_name = "some_class_name"
my_link = driver.find_element_by_class_name(link_class_name)

If you use By, then your initial locator might be

link_locator = (By.ID, "some_id")

and you locate your element as

my_link = find_element(*link_locator)

In case of changes in HTML source you need just to update your link_locator as

link_locator = (By.CLASS_NAME, "some_class_name")

and my_link remains exactly the same

Foretopmast answered 11/4, 2017 at 21:34 Comment(4)
Thanks for your answer. Seems like By can be used to make tests more flexible and easier to maintain. I am thoroughly confused by Page Object model and have not been able to wrap my head around the concept yet.Atlantic
By.*locator type* only just returns a string. Why not just run driver.find_element("css selector", "[css='something']"? It seems like By is mostly useless because we could easily just pass in those strings ourselves.Atlantic
Yeah.. All variables in By() class are simple strings and you can use "id" instead of By.ID, "xpath" instead of By.XPATH. I think it was made just to add little verbosity and also to simplify working in IDE- you can see available options when entering By., for example, in PyCharmForetopmast
how does one import this "By" ? -> ok got it from selenium.webdriver.common.by import By should mention thatRecalcitrate
M
2

Like above answer. I get the deprecated message when using find_element_by_id etc. So best way should be by using find_element().

Mezzotint answered 7/4, 2022 at 5:55 Comment(0)
N
1

Both of these methods are from the RemoteWebDriver class.

findElement(By.id()) requires you to created your own By.id instance.
findElementById(String) is a helper function that will generate the By.id
instance for you.

It comes down to providing you with the flexibility to choose what you want to keep track of in your tests/framework. Do you want to track locators as Strings? Do you want to track locators as By objects?

Newland answered 11/4, 2017 at 20:20 Comment(1)
OP is looking for an answer in Python. Your explanations are not applicable in this exact case as there are several differences in Python+Selenium and Java+SeleniumForetopmast
B
1

Also in the new python version, find_element_by_...() is removed, so just use find_element() + By

Barna answered 12/1, 2022 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.