how do I catch values returned from selenium web driver and store as one list

203 views Asked by At

I'm new to programming and I am using selenium to get data from scheduled matches from flashscore, I intend to assign each list returned from each loop to a variable so I can perform some calculations and also pop what I don't need.

Selenium web driver returns 2 elements on each for loop and the two elements are on different lists which makes it hard for me to access it or make the calculation

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Firefox()
url = 'https://www.flashscore.com/'
driver.get(url)
time.sleep(2)
#closing Accept terms
element = driver.find_element(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]').click()
#clicking on Scheduled matches
Scheduled = driver.find_element(By.XPATH, '/html/body/div[7]/div[1]/div/div[1]/div[2]/div[4]\
/div[2]/div/div[1]/div[1]/div[5]/div').click()
time.sleep(2)
# getting all Scheduled matchs

try:
    #finding scheduled matches
    Scheduled = driver.find_elements(By.XPATH, "//*[starts-with(@id, 'g_1')]")
    time.sleep(3)
except NoSuchElementException:
    pass

for value in Scheduled:
    window_home = driver.window_handles[0]
    value.click()
    time.sleep(3)
    window_1_open = driver.window_handles[1]
    driver.switch_to.window(window_1_open)
    driver.maximize_window()
    time.sleep(2)
    try:
        time.sleep(3)
        find_standing = driver.find_element(By.LINK_TEXT, 'STANDINGS')
        time.sleep(2)
        find_standing.click()
        time.sleep(3)
        get_match_values = driver.find_elements(By.XPATH,"//*[starts-with(@class, \
        'ui-table__row table__row--selected')]")
        time.sleep(2)
        for values in get_match_values:
            print(values.text)
        driver.close()
        driver.switch_to.window(window_home)
    except NoSuchElementException:
        driver.close()
        driver.switch_to.window(window_home)
1

There are 1 answers

0
JOJO On

I was able to resolve it by using index to catch each of the strings.

    team1_list = []
    team2_list = []
    team2 = get_match_values[1]
    team2_list.append(team2)
    time.sleep(2)
    team1 = get_match_values[0]
    team1_list.append(team1)
    time.sleep(2)
    for y in team1_list:
        sco = y.text
        sc = sco.split()