先自创一个html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>这是一个下拉框</title>
</head>
<body>
<div>SELECTED
<p>
请选择:
<select name="NR" id="nr">
<option value="10">每页显示10条</option>
<option value="20" selected>每页显示20条</option>
<option value="50">每页显示50条</option>
</select>
</p>
</div>
</body>
运行该文件
#coding=utf-8
from selenium import webdriver
import time as t
from selenium.webdriver.support.select import Select
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('file:///D:/1TestProject/wangyiyun/project18/selecthtml.html')
nr=driver.find_element_by_id('nr')
#实例化
select=Select(nr)
#索引的方式
#select.select_by_index(2)
#select.select_by_value('10')
select.select_by_visible_text('每页显示50条')
t.sleep(3)
driver.quit()
举例:拉勾网下拉框的选择方法
#coding=utf-8
from selenium import webdriver
import time as t
from selenium.webdriver.support.select import Select
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.lagou.com/')
t.sleep(5)
driver.find_element_by_id('search_input').send_keys('自动化测试')
t.sleep(3)
driver.find_element_by_id('search_button').click()
t.sleep(5)
#实现工作性质选择全职
#点击工作性质
driver.find_element_by_xpath('//*[@id="order"]/li/div[3]/div').click()
t.sleep(5)
#选择全职
driver.find_element_by_xpath('//*[@id="order"]/li/div[3]/div/ul/li[2]/a').click()
t.sleep(3)
driver.quit()