Home > Back-end >  Want to click download csv button using Selenium Python, but button changes class-name when hovered
Want to click download csv button using Selenium Python, but button changes class-name when hovered

Time:10-11

I'm trying to save a csv file by clicking the download csv button on a website. However, I noticed the .click() action not doing anything, and I found that the class-name of the button changes from 'export-button is-csv' to 'export-button is-csv hovering.' However, when I try find_element_by_class_name() on the new class name, it returns an error saying it's not there. Here's my code:

driver = webdriver.Chrome('chromedriver',options=options)

driver.get('https://www.rotowire.com/basketball/injury-report.php')

time.sleep(1)

download_csv=driver.find_element_by_class_name('export-button.is-csv')

download_csv.find_element_by_class_name('export-button.is-csv.hovering').click()

Here is the error message I receive:

Message: no such element: Unable to locate element: {"method":"css selector","selector":".export-button is-csv.hovering"}
  (Session info: headless chrome=94.0.4606.71)

Was wondering what the specific fix to this is(am using Google Colabs and am new to Selenium).

CodePudding user response:

Just get the table straight from the source then use pandas to convert to dataframe and write to disk:

import requests
import pandas as pd

url = 'https://www.rotowire.com/basketball/tables/injury-report.php?team=ALL&pos=ALL'
jsonData = requests.get(url).json()

df = pd.DataFrame(jsonData)
df.to_csv('file.csv', index=False)      

CodePudding user response:

You can directly use

button.is-csv

css selector to click on it.

driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.rotowire.com/basketball/injury-report.php')

time.sleep(1)

download_csv = driver.find_element_by_css_selector('button.is-csv')
download_csv.click()
  • Related