【Python+selenium Wendriver API】之下拉框定位
上代碼:
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.select import Select import timedriver = webdriver.Chrome() driver.get("https://www.baidu.com/") driver.implicitly_wait(20)mouse = driver.find_element_by_link_text("設置") ActionChains(driver).move_to_element(mouse).perform() driver.find_element_by_link_text("搜索設置").click() time.sleep(2) # 實例化select s = Select(driver.find_element_by_id("nr")) # 定位選項 s.select_by_value("20") # 選擇value="20"的項:通過value屬性 time.sleep(2) #為了明顯的看出變化 s.select_by_index(0) # 選擇第一項選項:通過選項的順序選擇,第一個為 0 time.sleep(2) #為了明顯的看出變化 s.select_by_visible_text("每頁顯示50條") # 選擇text="每頁顯示50條"的值,即在下拉時我們可以看到的文本?
-
Select提供了三種選擇方法:
select_by_index(index) ——通過選項的順序,第一個為 0?
select_by_value(value) ——通過value屬性?
select_by_visible_text(text) ——通過選項可見文本 -
Select提供了四種方法取消選擇:
deselect_by_index(index)?
deselect_by_value(value)?
deselect_by_visible_text(text)?
deselect_all() -
Select提供了三個屬性方法提供信息:
options ——提供所有的選項的列表,其中都是選項的WebElement元素?
all_selected_options ——提供所有被選中的選項的列表,其中也均為選項的WebElement元素?
first_selected_option ——提供第一個被選中的選項,也是下拉框的默認值 -
通過Select提供的方法和屬性,我們可以對標準select下拉框進行任何操作,但是對于非select標簽的偽下拉框,需要使用XPath定位;
-
?
其他文章:
https://blog.csdn.net/zwq912318834/article/details/79197114
https://www.cnblogs.com/fengyiru6369/p/7088836.html
轉載于:https://www.cnblogs.com/Owen-ET/p/9559229.html
總結
以上是生活随笔為你收集整理的【Python+selenium Wendriver API】之下拉框定位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cin 和 getline 混用中需要注
- 下一篇: JVM(3)——Java GC分析