Search code examples
pythonseleniumselenium-webdriverwebdrivergetattribute

Python Selenium access inner elements


Just for fun, I try to extract the scheduled games for day 1 of this NFL season. As of now, I have this code in Python

from selenium import webdriver

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

from webdriver_manager.chrome import ChromeDriverManager

browser = webdriver.Chrome(ChromeDriverManager().install())

from selenium.webdriver.chrome.options import Options

opts = Options()
opts.headless=True

#browser = driver(options=opts)
print("Start")

website = "https://www.nfl.com/schedules/2020/REG1"
browser.get(website)
gamedays = WebDriverWait(browser, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "nfl-o-matchup-group")))
   
print ("There are: ", len(gamedays))

This prints "There are: 3" which is correct because there are three sections having that class. How can I dig deeper into each section? For example, there is an element having the game date and of course, there are elements having the home team and the away team.

I have tried

   for j in range(0,len(gamedays)):
      game_on = gamedays[j].find_elements_by_class_name('d3-o-section-title')
      print(game_on)

which prints

[<selenium.webdriver.remote.webelement.WebElement (session="d8807c1ca013d7a2d58bd7377b42ca1a", element="034de32d-bf64-4544-94aa-d97ed6640367")>]

That is not helpful.

So, how can I get to that information?

Any help is appreciated.


Solution

  • Solution

    Loop through objects returned in game_on and print their innerHtml

    print(game_on.get_attribute("innerHTML"))
    

    OR

    for element in game_on:
       print(element.get_attribute("outerHTML"))