Python爬蟲(chóng)之Selenium下拉框處理的實(shí)現(xiàn)
在我們?yōu)g覽網(wǎng)頁(yè)的時(shí)候經(jīng)常會(huì)碰到下拉框,WebDriver提供了Select類(lèi)來(lái)處理下拉框,詳情請(qǐng)往下看:
本章中用到的關(guān)鍵方法如下:
select_by_value():設(shè)置下拉框的值 switch_to.alert.accept():定位并接受現(xiàn)有警告框(詳情請(qǐng)參考Python爬蟲(chóng) - Selenium(9)警告框(彈窗)處理) click():鼠標(biāo)點(diǎn)擊事件(其他鼠標(biāo)事件請(qǐng)參考Python爬蟲(chóng) - Selenium(5)鼠標(biāo)事件) move_to_element():鼠標(biāo)懸停(詳情請(qǐng)參考Python爬蟲(chóng) - Selenium(5)鼠標(biāo)事件)from selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.support.select import Selectimport timedriver = webdriver.Chrome()driver.get(’http://www.baidu.com’)# 鼠標(biāo)懸停至“設(shè)置”鏈接link = driver.find_element_by_link_text(’設(shè)置’)ActionChains(driver).move_to_element(link).perform()time.sleep(2) #睡兩秒,看一下效果# 打開(kāi)搜索設(shè)置driver.find_element_by_link_text('搜索設(shè)置').click()time.sleep(2) #睡兩秒,看一下效果# 搜索結(jié)果顯示條數(shù)sel = driver.find_element_by_xpath('//select[@id=’nr’]')Select(sel).select_by_value(’50’) # 顯示50條time.sleep(2) #睡兩秒,看一下效果# 保存設(shè)置driver.find_element_by_class_name('prefpanelgo').click()time.sleep(2) #睡兩秒,看一下效果# 定位并接受現(xiàn)有警告框alert = driver.switch_to.alert.accept()time.sleep(2) #睡兩秒,看一下效果driver.quit()
select類(lèi)中的函數(shù)列表
函數(shù) 解析 options 返回select元素所有的options all_selected_options 返回select元素中所有已選中的選項(xiàng) first_selected_option 返回select元素中選中的第一個(gè)選項(xiàng) select_by_index(index) 通過(guò)索引定位,index索引是從“0”開(kāi)始 select_by_value(value) 通過(guò)value屬性值定位 select_by_visible_text(text)t 通過(guò)文本值定位,visible_text是在option標(biāo)簽中間的值,即顯示在下拉框的值; deselect_all() 取消全部的已選擇項(xiàng) deselect_by_index(index) 取消已選中的索引項(xiàng) deselect_by_value(value) 取消已選中的value值 deselect_by_visible_text(text) 取消已選中的文本值
舉例
html如下:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>我是標(biāo)題</title></head><body><!--select標(biāo)簽--><select name='city' size='5' multiple='multiple'> <option value='1' tabindex='1'>北京</option> <option value='2' tabindex='2' selected='selected'>河南</option> <option value='3' tabindex='3'>河北</option> <option value='4' tabindex='4'>山東</option> <option value='5' tabindex='5'>上海</option></select></body></html>
from selenium import webdriverfrom selenium.webdriver.support.select import Selectimport timedriver = webdriver.Chrome(r'D:browserchromedriverchromedriver.exe')driver.get('http://localhost:63342/ui_test/select%E6%A0%87%E7%AD%BE.html')driver.maximize_window()ele = driver.find_element_by_name('city')select = Select(ele)select.select_by_value('3') # 選中'河北'time.sleep(3)select.select_by_index(0) # 選中'北京'time.sleep(3)select.deselect_by_value('3') # 取消選中'河北'time.sleep(3)select.deselect_by_index(0) # 取消選中'北京'time.sleep(3)driver.quit()
Selenium文集傳送門(mén):
標(biāo)題 簡(jiǎn)介 Python爬蟲(chóng) - Selenium(1)安裝和簡(jiǎn)單使用 詳細(xì)介紹Selenium的依賴環(huán)境在Windows和Centos7上的安裝及簡(jiǎn)單使用 Python爬蟲(chóng) - Selenium(2)元素定位和WebDriver常用方法 詳細(xì)介紹定位元素的8種方式并配合點(diǎn)擊和輸入、提交、獲取斷言信息等方法的使用 Python爬蟲(chóng) - Selenium(3)控制瀏覽器的常用方法 詳細(xì)介紹自定義瀏覽器窗口大小或全屏、控制瀏覽器后退、前進(jìn)、刷新瀏覽器等方法的使用 Python爬蟲(chóng) - Selenium(4)配置啟動(dòng)項(xiàng)參數(shù) 詳細(xì)介紹Selenium啟動(dòng)項(xiàng)參數(shù)的配置,其中包括無(wú)界面模式、瀏覽器窗口大小設(shè)置、瀏覽器User-Agent (請(qǐng)求頭)等等 Python爬蟲(chóng) - Selenium(5)鼠標(biāo)事件 詳細(xì)介紹鼠標(biāo)右擊、雙擊、拖動(dòng)、鼠標(biāo)懸停等方法的使用 Python爬蟲(chóng) - Selenium(6)鍵盤(pán)事件 詳細(xì)介紹鍵盤(pán)的操作,幾乎包含所有常用按鍵以及組合鍵 Python爬蟲(chóng) - Selenium(7)多窗口切換 詳細(xì)介紹Selenium是如何實(shí)現(xiàn)在不同的窗口之間自由切換 Python爬蟲(chóng) - Selenium(8)frame/iframe表單嵌套頁(yè)面 詳細(xì)介紹如何從當(dāng)前定位的主體切換為frame/iframe表單的內(nèi)嵌頁(yè)面中 Python爬蟲(chóng) - Selenium(9)警告框(彈窗)處理 詳細(xì)介紹如何定位并處理多類(lèi)警告彈窗 Python爬蟲(chóng) - Selenium(10)下拉框處理 詳細(xì)介紹如何靈活的定位并處理下拉框 Python爬蟲(chóng) - Selenium(11)文件上傳 詳細(xì)介紹如何優(yōu)雅的通過(guò)send_keys()指定文件進(jìn)行上傳 Python爬蟲(chóng) - Selenium(12)獲取登錄Cookies,并添加Cookies自動(dòng)登錄 詳細(xì)介紹如何獲取Cookies和使用Cookies進(jìn)行自動(dòng)登錄 Python爬蟲(chóng) - Selenium(13)設(shè)置元素等待 詳細(xì)介紹如何優(yōu)雅的設(shè)置元素等待時(shí)間,防止程序運(yùn)行過(guò)快而導(dǎo)致元素定位失敗 Python爬蟲(chóng) - Selenium(14)窗口截圖 詳細(xì)介紹如何使用窗口截圖 Python爬蟲(chóng) - Selenium(15)關(guān)閉瀏覽器 詳細(xì)介紹兩種關(guān)閉窗口的區(qū)別
到此這篇關(guān)于Python爬蟲(chóng)之Selenium下拉框處理的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Selenium 下拉框內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python自動(dòng)化之定位方法大殺器xpath2. python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別3. 如何基于Python和Flask編寫(xiě)Prometheus監(jiān)控4. Python Selenium破解滑塊驗(yàn)證碼最新版(GEETEST95%以上通過(guò)率)5. python 寫(xiě)一個(gè)文件分發(fā)小程序6. 用python對(duì)oracle進(jìn)行簡(jiǎn)單性能測(cè)試7. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟8. Python QT組件庫(kù)qtwidgets的使用9. Vue3中使用this的詳細(xì)教程10. Python中Anaconda3 安裝gdal庫(kù)的方法
