from
selenium.webdriver.support.select
import
Select
from
selenium
import
webdriver
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.Chrome()
driver.maximize_window()
driver.get(
"https://www.w3school.com.cn/tiy/t.asp?f=eg_html_elements_select"
)
wait
=
WebDriverWait(driver,
10
)
# 切换进入iframe
wait.until(EC.frame_to_be_available_and_switch_to_it((By.
ID
,
"iframeResult"
)))
# 实例化Select
s
=
Select(driver.find_element(By.XPATH,
'//select[@name="cars"]'
))
time.sleep(
2
)
# 选下拉列表的值 - value属性
s.select_by_value(
"audi"
)
time.sleep(
2
)
# 选下拉列表的值 - 索引 - 从0开始
s.select_by_index(
1
)
time.sleep(
2
)
# 选下拉列表的值 - 文本内容
s.select_by_visible_text(
"Fiat"
)
time.sleep(
5
)
driver.quit()