如何捕获从 Selenium Web 驱动程序返回的值并将其存储为一个列表

发布于 2025-01-17 18:21:50 字数 1744 浏览 1 评论 0原文

我是编程的新手,我正在使用Selenium从FlashScore的计划匹配中获取数据,我打算将每个循环返回的每个列表分配到一个变量中,以便我可以执行一些计算并弹出我不需要的内容。

Selenium Web驱动程序在每个循环中返回2个元素,并且两个元素在不同的列表上,这使我很难访问或进行计算

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-01-24 18:21:50

我能够通过使用索引来捕获每个字符串来解决它。

    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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文