skip to Main Content

I am trying to use Selenium in python to click a button on a webpage that leads to a pdf viewer. More specifically, I am trying to click that button labeled ‘Download Gamebook (pdf)’ on the web page ‘https://www.nfl.com/games/49ers-at-seahawks-2022-reg-15’ I don’t think I am correctly locating the button in the html or I am not using the correct selenium commands.

Here is what I have tried:

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import time

# Initialize the webdriver and navigate to the page
driver = webdriver.Chrome()
driver.get("https://www.nfl.com/games/49ers-at-seahawks-2022-reg-15")

# Wait for the page to load
time.sleep(5)

# Find the button element and click it

# other button ive tried
#button = driver.find_element(By.XPATH, "//div[contains(text(), 'Download Game Book (PDF)')]" )
#button = driver.find_element(By.LINK_TEXT, "Download Game Book (PDF)" )
#button = driver.find_element(By.PARTIAL_LINK_TEXT, "Download Game Book (PDF)" )
#button = driver.find_element(By.CLASS_NAME, "css-text-901oao r-alignItems-1awozwy r-color-1khnkhu r-display-6koalj r-flexDirection-18u37iz r-fontFamily-1fdbu1n r-fontSize-1b43r93" )
#button = driver.find_element(By.XPATH, "//button[text(), 'Download Game Book (PDF)']" )

button = driver.find_element(By.ID, "gamecenter-cta-btns-container" )
button.click()

# Wait for the PDF to download
time.sleep(10)

# Get the current URL and print it
url = driver.current_url
print(url)

# Close the webdriver
driver.quit()

When I run the code above I get the message: ‘selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="gamecenter-cta-btns-container"]"}’. I have not been able to find much information about this error and how to resolve it. I have tried other buttons as well that I have commented out but I get similar errors. I have tried resolving this with other questions on stack overflow but have had no luck. Below is a picture of the html of the button:

html of button

2

Answers


  1. For obtaining the PDF link it seems you need only the gameID number. You can get this number from the HTML source (using regex for example):

    import re
    import requests
    
    url = 'https://www.nfl.com/games/49ers-at-seahawks-2022-reg-15'
    pdf_url = 'https://static.www.nfl.com/gamecenter/{game_id}.pdf'
    html_doc = requests.get(url).text
    
    game_id = re.search(r"gameIDs*=s*'([^']+)'", html_doc).group(1)
    print(pdf_url.format(game_id=game_id))
    

    Prints:

    https://static.www.nfl.com/gamecenter/ba104950-d24c-11ec-b23d-d15a91047884.pdf
    
    Login or Signup to reply.
  2. I’ve tried with the XPATH and it should work.

    button = driver.find_element(By.XPATH, "//*[@id="main-content"]/div/div/div/div[2]/div/div[1]/div[2]/div[4]/div/div/div/div/div" )
    

    I’m not sure what your goal is. But there is more effective ways to get the link or to download the pdf.

    What i see from your code is that you are not copying the XPATH from the Dev Console. So you might try in the dev console to right click on the element your trying to interact with and select "copy" and "copy XPATH".

    I’m not an expert but i hope my answer helps you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search