python+selenium操作下拉框
以該網(wǎng)站為例:https://www.17sucai.com/pins/demo-show?id=5926
該網(wǎng)頁下存在多個(gè)可供測試的下拉框。
基本腳手架代碼:
from selenium.webdriver.support.ui import Select from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://www.17sucai.com/pins/demo-show?id=5926') # 切換到iframe driver.switch_to.frame(driver.find_element_by_id('iframe'))
定位下拉框(以第一個(gè)下拉框?yàn)槔?/p>
Select()方法
select_elm = Select(driver.find_element_by_class_name('nojs'))
不推薦這樣定位:
select_elm = driver.find_element_by_class_name('nojs').click()
選中下拉框,點(diǎn)擊:
driver.find_element_by_xpath('//option[@value="JP"]').click()
第一個(gè)下拉框的可選值如圖所示:
選擇下拉框中的值,有以下三種方法
1.根據(jù)索引選擇
select_elm.select_by_index(2)
符合索引規(guī)則,從0開始計(jì)數(shù),自動選擇了Canada
值。
2.根據(jù)值選擇
select_elm.select_by_value('CA')
這里的值指的是value
屬性,而不是文本字符串。
運(yùn)行結(jié)果同上。
3.根據(jù)可視的文本選擇
select_elm.select_by_visible_text('Canada')
即看到的是什么,選擇的就是什么。
運(yùn)行結(jié)果同上。
到此這篇關(guān)于python+selenium
操作下拉框的文章就介紹到這了,更多相關(guān)python selenium操作下拉框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于tensorflow和keras版本的對應(yīng)關(guān)系
這篇文章主要介紹了關(guān)于tensorflow和keras版本的對應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python實(shí)現(xiàn)識別手寫數(shù)字大綱
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)識別手寫數(shù)字的大綱,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01PyCharm設(shè)置SSH遠(yuǎn)程調(diào)試的方法
這篇文章主要介紹了PyCharm設(shè)置SSH遠(yuǎn)程調(diào)試的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07