selenium+python 下拉框選擇

aiee發表於2018-08-03

Select類提供了三種選擇某一選項的方法:


 
  1. select_by_index(index)

  2. select_by_value(value)

  3. select_by_visible_text(text)

 

 

from selenium import webdriverd

from selenium.webdriver.support.ui import Select

 

driver = webdriver.Firefox()

driver.get(url)

sl = Select(driver.find_element_by_id('s1Id')) 

sl.select_by_index(1) # 選擇第二項選項

sl.select_by_value("test") # 選擇value="test"的項

sl.select_by_visible_text("年紀") # 選擇text=年紀"的值

 

取消選擇:

deselect_by_index(index) 
deselect_by_value(value) 
deselect_by_visible_text(text) 
deselect_all()

 

 

相關文章