0
os.environ['PATH'] += 
   r"C:\Users\dew23\OneDrive\Computer Science"
driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://official.nba.com/nba-injury- 
report-2022-23-season/")
WebDriverWait(driver, 
   10).until(EC.presence_of_element_located((By.XPATH, 
   '//*[@id="main"]/div/section[1]/div/div/p/a[12]')))
driver.find_element(By.XPATH, '//*[@id="main"]/div/section[1]/div/div/p/a[12]').send_keys(Keys.RETURN)

the link gets clicked but it does not open the pdf file. how do I open the file in a new tab?

1 Answer 1

0

There are several issues here:

  1. The main issue causing your code to click the element but not to open the file is because you need to wait for element clickability. Element presence is a very first state when element is already presented but still not fully rendered. So, clicking a web element on that stage will just do nothing as you see yourself.
  2. No need to get the element again with driver.find_element(By.XPATH, '//*[@id="main"]/div/section[1]/div/div/p/a[12]') after you already applied WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/div/section[1]/div/div/p/a[12]'))) since the former method returns a web element object.
  3. Long '//*[@id="main"]/div/section[1]/div/div/p/a[12]' XPath expression can be changed by this XPath "//a[contains(@href,'2022-11-22_11AM')]" it is much more precise and reliable.

So, the final code can be like this:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://official.nba.com/nba-injury-report-2022-23-season/"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href,'2022-11-22_11AM')]"))).click()

And it woks, the result is

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your thorough explanation. when I run the final code you offered, I get the same result. the pdf does not open. any other suggestions? the code I am running is the exact same as yours
I run it on my PC several times and it worked just fine.
I am using VScode. would that be causing a problem?
I don't know...
that's alright, thank you for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.