准备
selenium版本3.8.0
python版本3.8.5
selenium可以使用webdriver来自动化操作浏览器
from selenium import webdriver
要下载浏览器的driver应用程序到python的安装路径
火狐浏览器的是geckodriver.exe
火狐浏览器版本为93.0 (64 位)
geckodriver的版本为geckodriver-v0.30.0-win64
IEDriverServer的版本为 3.8.0 win32位的
iedriver不能使用以下
js = "window.open('http://www.baidu.com')" driver.execute_script(js) # 执行js代码(打开新窗口)
IEDriverServer不如geckodriver好用,会存在各种奇怪的问题
浏览器设置
# 设置火狐浏览器自动保存下载文件
profile = webdriver.FirefoxProfile() # 火狐浏览器配置文件
profile.set_preference('browser.download.folderList', 2) # 设置成 2 表示使用自定义下载路径
profile.set_preference('browser.download.dir', dest_path) # 指定下载保存路径
profile.set_preference('browser.download.manager.showWhenStarting', False) # 在开始下载时是否显示下载管理器
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', # 对所给出文件类型不再弹出框进行询问
'application/octet-stream,application/vnd.ms-excel,text/csv,'
'application/zip,text/html,application/xhtml+xml,application/xml,'
'application/x-download,application/x-msdownload,application/x-excel')
# 应用配置文件,使用火狐浏览器
driver = webdriver.Firefox(firefox_profile=profile)
# 隐含等待20秒
driver.implicitly_wait(20)
# 最大化窗口
driver.maximize_window()
常见命令
打开
driver.get(url) # 打开指定url的页面
定位元素
driver.find_element_by_xpath('//*[@id="su"]').click() # 根据页面元素的xpath值定位页面元素,并点击
div_class = driver.find_element_by_xpath('/html/body/div').get_attribute('class') # 获取页面元素的class属性的值
窗口
handles = driver.window_handles # 获取浏览器所有窗口句柄
driver.switch_to.window(handles[1]) # 调到第1个窗口
print('handles[1]:', driver.current_url) # 当前窗口的url
driver.close() # 关闭窗口
driver.switch_to.window(handles[0]) # 调到第0个窗口
选择框
start = Select(driver.find_element_by_name('STARTCYCLE')) # 根据name定位select元素
start.select_by_value('202107)(202107') # 根据value选择项
页面框架
有时页面元素会处于一层或多层frame中,需要进入frame中找,不然找不到
driver.switch_to.frame('layui-layer-iframe1') # 进入frame
输入
driver.find_element_by_xpath('//*[@id="password"]').send_keys(user_pwd) # 定位元素后,输入
退出
driver.quit() # 会关闭所有由driver打开的页面