Issues returning multiple values in crawling script

Viewed 25

This is my current code. The time portion is working great, the distance portion seems to be working well as well, but the heart rate portion is a bit wonky still.

I am unsure how to return two separate values from the function. I guess it needs two separate arguments then? I tried "return x and y", but it didn't work very well.

If I can return two values, I should also be able to extract two values which would make the script a lot less clunky, because then I won't have to search for the same activity multiple times.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import login as login
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import time

x = datetime.datetime.now()
x = x.strftime("%b %d")

driver = browser = webdriver.Firefox()
driver.set_window_size(1512, 1000)
driver.get("https://connect.garmin.com/modern/activities")

driver.implicitly_wait(1)

iframe = driver.find_element(By.ID, "gauth-widget-frame-gauth-widget")
driver.switch_to.frame(iframe)

driver.find_element("name", "username").send_keys(login.username)

driver.find_element("name", "password").send_keys(login.password)
driver.find_element("name", "password").send_keys(Keys.RETURN)

driver.switch_to.default_content()

time.sleep(10)

def get_time_xpath(value):
    return "//span[text() = '{}']//ancestor::div[@class='list-item-container']//div[5]//div[2]//span//span[1]".format(value)

def get_distance_xpath(value):
    return "//span[text() = '{}']//ancestor::div[@class='list-item-container']//div[5]//div[1]//span//span[1]".format(value)

def get_hr_xpath(arg):
    return "//span[text() = '{}']//ancestor::div[@class='list-item-container']//div[5]//div[4]//span//span".format(arg)


distance_str = get_distance_xpath(x)

date_str = get_time_xpath(x)

hr_str = get_hr_xpath(x)


# def get_time_from_page(activity):
# 
#   time.sleep(2)
# 
#   driver.find_element("name", "search").clear()
#   driver.find_element("name", "search").send_keys(activity)
#   driver.find_element("name", "search").send_keys(Keys.RETURN)
# 
#   time.sleep(3)
#   result_1 = 0
#   current_time = driver.find_elements(By.XPATH, date_str)
# 
#   for times in current_time:
#       if len(times.text) >= 7:
#           result = time.strptime(times.text, "%H:%M:%S")
#           result_1 += result.tm_hour * 60
#           result_1 += result.tm_min
#       else:
#           result = time.strptime(times.text, "%M:%S")
#           result_1 += result.tm_min
# 
#   return result_1


def get_distance_and_hr(activity):

    time.sleep(2)

    driver.find_element("name", "search").clear()
    driver.find_element("name", "search").send_keys(activity)
    driver.find_element("name", "search").send_keys(Keys.RETURN)

    time.sleep(3)
    result_2 = 0.00
    hr_value = 0
    distance = driver.find_elements(By.XPATH, distance_str)
    heart_rate = driver.find_elements(By.XPATH, hr_str)

    for dist in distance:
        remove = float(dist.text.rstrip(" km"))
        result_2 += remove

    for val in heart_rate:
        reduce = float(val.text.rstrip(" bpm"))
        hr_value += reduce

    return result_2 and hr_value



# time_read = get_time_from_page("Reading")
# time_meditated = get_time_from_page("Meditation")
# time_programmed = get_time_from_page("Programming")
# time_walked = get_time_from_page("Walking")
# time_running = get_time_from_page("Running")
distance_walked = get_distance_and_hr("Walking")
heart_rate_walk = get_distance_and_hr("Walking")

# print(time_read)
# print(time_meditated)
# print(time_programmed)
# print(time_walked)
# print(time_running)
print(f"Distance walked today is {distance_walked} km and average BPM is {heart_rate_walk}") ```

1 Answers

when you return value1 and value 2 the function will return only the last value.

Instead, when you have multiple items to return you can return them in a tuple and extract them from it.

You can try doing:

return result_2, hr_value

#or

return (result_2, hr_value)

#or 

return tuple(result_2, hr_value)
Related