如何在Selenium中利用检查元素处理下拉选择与标签切换下载文件
Selenium 补充实现登录后下拉选择、标签切换及文件下载流程
已完成登录逻辑,以下是补充后续操作的完整代码及步骤说明:
完整代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait, Select from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument("start-maximized") # 路径使用原始字符串避免转义问题 webdriver_service = Service(r'C:\webdrivers\chromedriver.exe') driver = webdriver.Chrome(service=webdriver_service, options=options) wait = WebDriverWait(driver, 20) url = "products.markit.com/home/login.jsp" driver.get(url) # 登录操作 wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("Admin") wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys("admin123") wait.until(EC.element_to_be_clickable((By.TAG_NAME, "button"))).click() # 1. 选择下拉菜单中的「Pricing Data - Loan」 # 下拉框id为productSelect,用Select类处理下拉选择 product_select = Select(wait.until(EC.visibility_of_element_located((By.ID, "productSelect")))) product_select.select_by_visible_text("Pricing Data - Loan") # 2. 切换至「Past Loan Market」标签 wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Past Loan Market"))).click() # 3. 点击「Download」标签触发下载 wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))).click() # 可选:若需等待下载完成后关闭浏览器,可添加等待逻辑(示例) # import time # time.sleep(10) # 根据文件大小调整等待时长 # driver.quit()
步骤说明
- 下拉菜单选择:通过
Select类处理下拉控件,等待下拉框可见后,精准匹配选项文本完成选择; - 标签切换:等待目标标签可点击后触发点击,切换到对应页面;
- 下载触发:等待下载标签可点击后点击,启动文件下载流程。
内容的提问来源于stack exchange,提问作者user20432746
相关产品推荐
相关产品推荐

