我想使用python下载页面末尾显示的csv数据:
https://www.cboe.com/delayed_quotes/spx/quote_table/
具体来说,在下载数据之前,我需要 python 将下拉列表 Option Range 和 Expirations 选择为“All”
我有以下代码通过python访问该网站,效果很好。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('https://www.cboe.com/delayed_quotes/spx/quote_table')
html = driver.page_source
我想使用selenium select功能,但我无法在html代码中找到两个下拉菜单的id
element = driver.find_element(By.ID, "????")
首先,如果下拉列表没有 <select>
或
<option>
元素,则不能使用 Selenium 的 Select 类。请参阅链接了解使用
Select
类可以做什么和不能做什么。
您的目标 HTML 没有用于目标下拉列表的
<select>
节点。因此,您需要以其他方式与下拉菜单进行交互。
请参阅以下工作代码以及注释行中的说明:
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://www.cboe.com/delayed_quotes/spx/quote_table")
# Click on I Agree cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='I agree']"))).click()
# Below 2 lines will send text 'All' to 'Options Range' text box and performs click on key TAB
wait.until(EC.element_to_be_clickable((By.ID, "select_5"))).send_keys("All")
wait.until(EC.element_to_be_clickable((By.ID, "select_5"))).send_keys(Keys.TAB)
# Below 2 lines will send text 'All' to 'Expiration' text box and performs click on key TAB
wait.until(EC.element_to_be_clickable((By.ID, "select_7"))).send_keys("All")
wait.until(EC.element_to_be_clickable((By.ID, "select_7"))).send_keys(Keys.TAB)
# Click on download button
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@download='spx_quotedata.csv']"))).click()
time.sleep(5)