AspriringProgrammer
AspriringProgrammer

Reputation: 21

Trouble Finding Element by Xpath For Downloading A File Off Site w/Selenium

driver.get("https://www.scb.se/hitta-statistik/statistik-efter-amne/finansmarknad/finansmarknadsstatistik/finansmarknadsstatistik/")
driver.find_element_by_xpath('//*[@id="pageContent"]/div[1]/article/div/div[1]/p[1]/a').click()

I'm trying to figure out how to download file "Finansmarknadsstatistik, november 2021" under Tabeller och diagram from the site in driver.get with Python/Selenium, through xpath, but I am unfamiliar with HTML and the correct identifiers to obtain the file and save it to my directory.

I don't know what div[1] etc.. is and I need some help with identifying how to obtain the file.

All advice is appreciated! Thank you.

Upvotes: 0

Views: 851

Answers (1)

Anand Gautam
Anand Gautam

Reputation: 2101

This should download the excel file

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

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.scb.se/hitta-statistik/statistik-efter-amne/finansmarknad/finansmarknadsstatistik/finansmarknadsstatistik/")
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "(//h2[@id='_Tabellerochdiagram']/..//ul//a)[1]"))).click()

You may use a little easier, but probably a less reliable locator startegy in lieu of the last line of above co:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "((//*[contains(text(), 'Finansmarknadsstatistik,')])[2]"))).click()

Upvotes: 1

Related Questions