簡體   English   中英

想要使用 Selenium Python 單擊下載 csv 按鈕,但是懸停時按鈕會更改類名?

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

我正在嘗試通過單擊網站上的下載 csv 按鈕來保存 csv 文件。 但是,我注意到 .click() 操作沒有執行任何操作,並且我發現按鈕的類名從“export-button is-csv”更改為“export-button is-csv hovering”。 但是,當我在新的 class 名稱上嘗試 find_element_by_class_name() 時,它會返回一個錯誤,指出它不存在。 這是我的代碼:

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

這是我收到的錯誤消息:

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)

想知道對此的具體修復是什么(我正在使用 Google Colabs 並且是 Selenium 的新手)。

只需直接從源中獲取表格,然后使用 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)      

你可以直接使用

button.is-csv

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