Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver Python page object MainPageLocators class why does it use an asterisk infront of the class name

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

like image 205
Riaz Ladhani Avatar asked Oct 18 '22 23:10

Riaz Ladhani


1 Answers

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)
like image 112
unutbu Avatar answered Oct 21 '22 17:10

unutbu