How to make one function with different behaviors based on calls?

Question:

I have a function and 2 for loops which I run with predefined list and the below function.

def my_function(pickup_d_str, return_d_str, loc, loc2):
        driver.find_element(By.XPATH,'//button[@aria-label="Edit pick-up & return"]').click()
        driver.find_element(By.XPATH,'//div[@class="chicklet location-chicklet-clear"]').click()
        driver.find_element(By.XPATH,'//input[@id="pickupLocationTextBox"]').send_keys(loc)
        driver.find_element(By.XPATH,'//li[@id="location-'+ loc2 +'"]').click()
        driver.find_element(By.XPATH,'//button[@id="continueButton"]').click()
    20 more lines after this


for l in locationList:
    my_function(pickup_d_str, return_d_str, l.loc_pri, l.loc_nearby)

I also have a 2nd list which I have to loop over and I will be using the same function. But I need to add the following lines in the function for this 2nd list.

for l in locationList2:
    my_function(pickup_d_str, return_d_str, l.loc_pri, l.loc_nearby)

In the above my_function, I have to add the following line because the webpage uses another search form

WebDriverWait(driver, 20).until( EC.visibility_of_element_located((By.XPATH, '//button[@id="check-availability"]'))).click()
driver.find_element(By.XPATH,'//div[@id="select-item-0"]//button[@aria-label="Select"]').click()
WebDriverWait(driver, 20).until( EC.visibility_of_element_located((By.XPATH, '//div[@id="search"]'))).click()

How can I effeciently tackle this? I tried adding these lines in a try and except in my current function than my script would always add an extra 30 seconds to my 1st for loop.

Asked By: Singh

||

Answers:

I think this problem would be solved by adding an extra argument and an if statement to the function, like so:

def my_function(pickup_d_str, return_d_str, loc, loc2, loop_id):
        driver.find_element(By.XPATH,'//button[@aria-label="Edit pick-up & return"]').click()
        driver.find_element(By.XPATH,'//div[@class="chicklet location-chicklet-clear"]').click()
        driver.find_element(By.XPATH,'//input[@id="pickupLocationTextBox"]').send_keys(loc)
        driver.find_element(By.XPATH,'//li[@id="location-'+ loc2 +'"]').click()
        driver.find_element(By.XPATH,'//button[@id="continueButton"]').click()
    20 more lines after this
    
    if loop_id == 2:
         WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//button[@id="check-availability"]'))).click()
         driver.find_element(By.XPATH,'//div[@id="select-item-0"]//button[@aria-label="Select"]').click()
         WebDriverWait(driver, 20).until( EC.visibility_of_element_located((By.XPATH, '//div[@id="search"]'))).click()
    
    else:
      pass
      

then function calls would be like:

for l in locationList:
    my_function(pickup_d_str, return_d_str, l.loc_pri, l.loc_nearby, loop_id=1)


for l in locationList2:
    my_function(pickup_d_str, return_d_str, l.loc_pri, l.loc_nearby, loop_id=2)

For the guys who want a simple explanation of ‘how to make one function with different behaviors based on calls, I wrote this simple code:

def function (a, b, id):
    if id==1:
        return a + b
    if id==2:
        return a - b

answer1 = function(3,2,id=1)
answer2 = function(3,2,id=2)

print(answer1)
print(answer2)

answer1 = 5

answer2 = 1

I advise you to change the title of your question so more people can be helped. I offer this title: "how to make one function with different behaviors based on calls?"

Answered By: Dark Dragon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.