0

I made a method to click on the calendar field and then choose the date, but when I move the parameters, is returning error.

My method

def select_current_date(self, *locator1, *locator2):

    self.driver.find_element(*locator1).click()
    WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, ".ui-icon.ui-icon-circle-triangle-w")))
    self.driver.find_element(*locator2).click()
    WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
        EC.invisibility_of_element_located((By.CSS_SELECTOR, ".ui-datepicker-title")))

Passing the parameters

self.select_current_date(*EventsLocators.RECEIVED, *EventsLocators.CURRENT_DATE)

My error

E     File "/Users/rafael/Desktop/projects/automated_tests/base.py", line 23
E       def select_current_date(self, *locator1, *locator2):
E                                                ^
E   SyntaxError: invalid syntax

Any idea?

Cheers!

Rafael C.
  • 2,245
  • 4
  • 29
  • 45

3 Answers3

3

You don't need (and actually you cannot do it this way) to unpack the arguments, replace:

def select_current_date(self, *locator1, *locator2):

with just:

def select_current_date(self, locator1, locator2):

When calling the method, now simply use:

self.select_current_date(EventsLocators.RECEIVED, EventsLocators.CURRENT_DATE)
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @Rafael cause you need to remove the * when calling the method. – alecxe Dec 08 '16 at 19:23
  • @Rafael and, looks like you might not need the * when actually using `locator1` and `locator2` inside the method. – alecxe Dec 08 '16 at 19:23
  • Weird, because when I pass just one parameters, work fine. Example from another method that I use: ` def clear_sendkeys(self, value, *locator): self.driver.find_element(*locator).clear() self.driver.find_element(*locator).send_keys(value)` The problem is using two parameters – Rafael C. Dec 08 '16 at 19:24
  • @Rafael could you please post what are the `EventsLocators.RECEIVED` and `EventsLocators.CURRENT_DATE` values? Thanks. – alecxe Dec 08 '16 at 19:25
  • Yes, I tried it but I'm getting the error `InvalidSelectorException: Message: Invalid locator values passed in` When selenium will interact in this field – Rafael C. Dec 08 '16 at 19:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130148/discussion-between-rafael-and-alecxe). – Rafael C. Dec 08 '16 at 19:31
1

If you read the docs for your method you'll see:

find_element(by='id', value=None)

That means find_element needs two parameters. I'm going to go out on a limb and guess that EventsLocators.RECEIVED is a 2-element list or tuple. If it's not, then this won't work at all. But what you actually need to do is just remove the *s from your function definition and function call:

def select_current_date(self, locator1, locator2):

    self.driver.find_element(*locator1).click()
    WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, ".ui-icon.ui-icon-circle-triangle-w")))
    self.driver.find_element(*locator2).click()
    WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
        EC.invisibility_of_element_located((By.CSS_SELECTOR, ".ui-datepicker-title")))

thing_one = ['by', 'value']
thing_two = ['replace', 'these things']
self.select_current_date(thing_one, thing_two)

I don't know the particulars of what you're passing in, but they must be some kind of iterable, otherwise your function signatures won't match.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
-3

Python doesn't have pointers so you don't need * there

edilio
  • 1,778
  • 14
  • 13