下拉???
下拉是指如下图的下拉选项,当我们要进行切换时,需要先点击下拉框处,再进行选择
以艺恩为例:
-
定位到下拉列表
sel_el = web.find_element_by_xpath(‘//*[@id=“OptionDate”]’)
-
对元素进行包装,包穿成下拉菜单
sel = Select(sel_el) -
进行切换
- 按照索引进行切换
sel.select_by_index(i) # i就是索引值- 按照元素value进行切换
sel.select_by_value(value)- 按照text进行切换
sel.select_by_visible_text(text)
注意:
每次切换的时候会发出网络请求,需要加载的时间
无头浏览器
无头浏览器:指的是在进行get操作时,不会弹出浏览器窗口
怎样才能获得无头浏览器?
以Chrome浏览器为例:对Chrome进行一个配置
- 导入包
from selenium.webdriver.chrome.options import Options
- 准备参数配置
opt = Options()
opt.add_argument(‘–headless’)
opt.add_argument(‘–disbale-gpu’)
- 参数设置到浏览器中
web = Chrome(options=opt)
实例
import time
from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select
#无头浏览器:运行的时候不会弹出浏览器窗口
#如何设置无头浏览器?
#对Chrome进行一个配置
# 1.导入包
from selenium.webdriver.chrome.options import Options
# 2. 准备参数配置
opt = Options()
opt.add_argument('--headless')
opt.add_argument('--disbale-gpu')
# 3. 参数设置到浏览器中
web = Chrome(options=opt)
# web = Chrome()
web.get('https://www.endata.com.cn/BoxOffice/BO/Year/index.html')
# 定位到下拉列表
sel_el = web.find_element_by_xpath('//*[@id="OptionDate"]')
#对元素进行包装,包穿成下拉菜单
sel = Select(sel_el)
# 让浏览器进行调整选项
for i in range(len(sel.options)): #这里的i就是每一个下拉框选项的索引位置
sel.select_by_index(i) # 按照索引进行切换
#注意:每次切换的时候会发出网络请求,需要加载的时间
time.sleep(2)
table = web.find_element_by_xpath('//*[@id="TableList"]/table')
print(table.text)
print('================')
print('程序运行完毕')
web.close()