There are several issues here:
- 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.
- 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.
- 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
