问题
I'm just reading this documentation here and was curious: What is the difference between public and private methods in this context?
To find multiple elements (these methods will return a list):
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
Apart from the public methods given above, there are two private methods which might be useful with locators in page objects. These are the two private methods: find_element and find_elements.
I don't understand why some of those are public methods, whereas others are private methods, and it isn't explained anywhere.
From doing a test, I've noticed that there are differences in the FirefoxWebElement for public and private selectors.
find_element_by_xpath('//whatever') returns
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="234a0c66-870f-4fee-92b5-8d10541f2d2d", element="3275635e-614d-42da-95ac-306b02743bec")>
session="234a0c66-870f-4fee-92b5-8d10541f2d2d"element="3275635e-614d-42da-95ac-306b02743bec"
find_element(By.XPATH, '//whatever') returns
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="c792073f-08b3-4519-a563-0f1e272a17e7", element="b61a6d2d-2c35-4872-a8e0-2649c189829b")>
session="c792073f-08b3-4519-a563-0f1e272a17e7"element="b61a6d2d-2c35-4872-a8e0-2649c189829b"
I'm not surprised that session is different, but why does it return a different value for element?!
回答1:
If you look at the definition of find_element_by_xpath
def find_element_by_xpath(self, xpath):
return self.find_element(by=By.XPATH, value=xpath)
It uses the find_element method. Now why is find_element documented as private? Well few things
- The initial developer considered had personal preference to code it that way
- How would you find when you have source codes using
find_element_by_xpathand some usingfind_element(By.XPATH, "//div")? Confusing, so best is to just keep it consistent - Python is usually used in more english like manner, so
find_element_by_xpathclear explains the intent - Can one use
find_elementinstead offind_element_by_XXXX? Why not, In my selenium helper library I use that only.
So those are my thoughts on the same, the original contributor to those line could probably tell his intentions were will making the method private
回答2:
There are two ways to get web element :
element = driver.find_element_by_id("some static id")
and :
element = driver.find_element(By.ID, 'some static id')
More or less they both are returning a web element.
So, find_element() which is a private method have some advantages such as :
If in case you want to locate the same element by CSS_SELECTOR or XPATH for new builds :
You would do something like :
element = driver.find_element_by_css_selector("some css selector")
but in case of find_element() private method :
element = driver.find_element(By.CSS_SELECTOR, 'some css selector')
As the official docs say : private methods which might be useful with locators in page objects.
Page object means for maintainability of your project and you will have your locators in either an INI file or in Page factory. So using Private method if you have to change something while doing regression or something , it would be easy to change the value at only one place.
来源:https://stackoverflow.com/questions/51035651/difference-between-public-and-private-selector-methods