简体   繁体   English

想要使用 Selenium Python 单击下载 csv 按钮,但是悬停时按钮会更改类名?

[英]Want to click download csv button using Selenium Python, but button changes class-name when hovered over?

I'm trying to save a csv file by clicking the download csv button on a website.我正在尝试通过单击网站上的下载 csv 按钮来保存 csv 文件。 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.'但是,我注意到 .click() 操作没有执行任何操作,并且我发现按钮的类名从“export-button is-csv”更改为“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.但是,当我在新的 class 名称上尝试 find_element_by_class_name() 时,它会返回一个错误,指出它不存在。 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).想知道对此的具体修复是什么(我正在使用 Google Colabs 并且是 Selenium 的新手)。

Just get the table straight from the source then use pandas to convert to dataframe and write to disk:只需直接从源中获取表格,然后使用 pandas 转换为 dataframe 并写入磁盘:

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)      

You can directly use你可以直接使用

button.is-csv

css selector to click on it. css selector点击它。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM