Difference Between Public and Private Selector Methods

痞子三分冷 提交于 2020-01-06 06:26:05

问题


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_xpath and some using find_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_xpath clear explains the intent
  • Can one use find_element instead of find_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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!