Home > Blockchain >  Python Selenium; getting data from value 'data-label'
Python Selenium; getting data from value 'data-label'

Time:11-11

Webpage with HTML inspect

try:
    data = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "DataTables_Table_5"))
    )
    scores = data.find_elements_by_tag_name('tbody')
    for score in scores:
        finalScores = score.find_element(By.Name, "Score")
        print(finalScores.text)
except:
    driver.quit()

I need to get the score value under the selected tag (see image). I have tried filtering by td and tr. I am new to selenium. Page URL: https://fgcuathletics.com/sports/womens-soccer/stats/2022

CodePudding user response:

Try this:

# Needed libs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# We create the driver
driver = webdriver.Chrome()

# We maximize the window
driver.maximize_window()

# We navigate to the url
url='https://fgcuathletics.com/sports/womens-soccer/stats/2022'
driver.get(url)

scores = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//table[@id='DataTables_Table_5']//tbody//td[@data-label='Score']")))
for score in scores:
    print(score.get_attribute('textContent'))
  • Related