我正在嘗試通過單擊網站上的下載 csv 按鈕來保存 csv 檔案。但是,我注意到 .click() 操作沒有做任何事情,我發現按鈕的類名從“export-button is-csv”更改為“export-button is-csv hovering”。但是,當我在新類名上嘗試 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 Colab 并且我是 Selenium 的新手)。
uj5u.com熱心網友回復:
只需直接從源獲取表,然后使用 Pandas 轉換為資料幀并寫入磁盤:
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)
uj5u.com熱心網友回復:
你可以直接使用
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313623.html
