WebUI測試-獲取html頁面表格資料並存到Excel中

特昂糖-發表於2024-04-08
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

table=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'table'))) #表格元素
rows = table.find_elements(By.CSS_SELECTOR, "tbody tr") #表資料行
thead_rows = table.find_elements(By.CSS_SELECTOR, "thead tr th") #表頭

data=[]
for i in range(len(rows)):
	columns = rows[i].find_elements(By.CSS_SELECTOR, 'td')
	row_data=[]
	for col in range(len(columns)):
		row_data.append(columns[col].text)
	data.append(row_data)
	df = pd.DataFrame(data, columns=[thead_rows[col].text for col in range(len(thead_rows))])
	df.to_excel('output.xlsx', index=False)

  

相關文章