1

I've been learning how to use selenium to parse data and I've been doing alright with that process. So I'm trying something different, in that I found data ta parse, but there is a provided export button which to me, sounds like a quicker solution, so I thought I'd have a stab at it. But I'm not quite understanding how it's not working:

browser = webdriver.Chrome()
url = 'https://www.rotowire.com/football/injury-report.php'
browser.get(url) 

button = browser.find_elements_by_xpath('//*[@id="injury-report"]/div[2]/div[2]/button[2]')
button.click()

browser.close()

I just want to click on the export csv button on the page. Also, I haven't looked yet, but my next step would be to specify where to save the csv file it exports. Right now it, defaults to the downloads folder. Is there a way to specify a location without changing the default? Also is there a way to specify a file name?

chitown88
  • 27,527
  • 4
  • 30
  • 59
  • hey, use this alternative xpath for button `browser.find_element_by_xpath('//img[@alt="CSV"]')` – Stack Sep 02 '18 at 08:44

1 Answers1

1

Try below code to click required button:

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

browser = webdriver.Chrome()
url = 'https://www.rotowire.com/football/injury-report.php'
browser.get(url) 

button = wait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "is-csv")))
button.click()

browser.close()

Also check how to save file to specific folder

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • works almost perfect. it downloads it, but do you know why it doesn't exactly download in the same format as manually doing it? I was thinking it would open exact the the same, but doing it through selenium, there's still some formatting i would need to do with the tables/strings to separate into the individual columns. – chitown88 Sep 02 '18 at 10:44
  • Hm.. I don't see any difference... Maybe try to download Excel file to preserve formatting – Andersson Sep 02 '18 at 10:50
  • ah. ok. ya the excel preserved the format I needed. thanks for the heads up. – chitown88 Sep 02 '18 at 10:58