I have been following the page object model for my Selenium Webdriver tests in Python. I have followed the sample from GitHub. URL is: https://github.com/baijum/selenium-python/blob/master/source/page-objects.rst
When you call the locator from the MainPageLocators class e.g from the URL.
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
It uses an asterisk in front of the class name *MainPageLocators. Why does it use * ?
It does not work if you use MainPageLocators, you have to use *MainPageLocators.
This is no good because when i use the WebDriverWait it does not work with *MainPageLocators or MainPageLocators. E.g.
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((*MainPageLocators.locator)))
I have to to do it this way for it to work which defeats the purpose of having the locators in one place.
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'button_id')))
Why the asterisk in front of MainPageLocators? Why does *MainPageLocators not work inside the WebDriverWait? It does work if you do
self.driver.find_element(*MainPageLocators.locator)
But it does not work if you use it in the WebDriverWait
Thanks, Riaz
In this context, *
is the argument-unpacking operator. It tells Python to unpack the values in the sequence that follows and pass them as arguments to the function. For example,
foo(*(1, 2, 3))
is equivalent to
foo(1, 2, 3)
Since MainPageLocators.GO_BUTTON
is defined like this:
class MainPageLocators(object):
"""A class for main page locators. All main page locators should come here"""
GO_BUTTON = (By.ID, 'submit')
it follows that find_element(*MainPageLocators.GO_BUTTON)
is equivalent to
find_element(By.ID, 'submit')
This works since find_element
expects 2 arguments.
In contrast, EC.element_to_be_clickable
expects a single 2-tuple as its argument. Therefore you would not want to use the argument-unpacking operator here. Instead, just pass the 2-tuple directly:
wait = WebDriverWait(self.driver, 20)
element = wait.until(EC.element_to_be_clickable((By.ID, 'submit'))
or use
element = wait.until(EC.element_to_be_clickable(MainPageLocators.GO_BUTTON)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With