from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select import time driver = webdriver.Firefox() test_url = "http://sahitest.com/demo/selectTest.htm" driver.get(test_url) driver.maximize_window() time.sleep(10) # 定位下拉列表的位置 list1 = driver.find_element(By.ID, "s1Id") # 实例化下拉列表 s1 = Select(list1) # 根据索引选中第二个列表选项 s1.select_by_index(1) # 根据列表选项值选中第三个选项 s1.select_by_value("o2") # 根据列表选项的文本选中第三个选项 s1.select_by_visible_text("o3") time.sleep(3) driver.close()