selenium库的使用
1 安装
pip install selenium
2 安装浏览器驱动
edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
谷歌: http://npm.taobao.org/mirrors/chromedriver/
驱动的版本要小于等于你浏览器的版本
3 模拟登录百度(简单使用)
from selenium import webdriver
bor=webdriver.Edge(executable_path='./msedgedriver.exe')
bor.get('https://www.baidu.com')
print(bor.page_source)
4 常用方法
4.1 浏览器对象
bro.get('https://www.baidu.com/') # 当前页跳转到百度
bro.implicitly_wait(5) # 隐士等待:找一个控件,如果控件没有加载出来,等待5s
bro.find_element_by_id('kw') # 返回id值为kw的控件
bro.find_element_by_link_text # 通过a标签内容找
bro.find_element_by_partial_link_text # 通过a标签内容找,模糊匹配
bro.find_element_by_tag_name # 标签名
bro.find_element_by_class_name # 类名
bro.find_element_by_name # name属性
bro.find_element_by_css_selector # 通过css选择器
bro.find_element_by_xpath # 通过xpaht选择器
find_elements_by_xxx的形式是查找到多个元素,结果为列表
bro.close() # 关闭浏览器
4.1 空间对象
# 除了bro.find_element_by_xpath和控件.find_element_by_xpath查找内容相同以外,其他查询均可以正常查询
tag.get_attribute('href') # 找当前控件 的href属性对的值
print(tag.id) # 当前控件id号
print(tag.location) # 当前控件在页面位置
print(tag.tag_name) # 标签名
print(tag.size) #标签的大小
5 无界面浏览器
5.1 谷歌
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000') # 指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
chrome_options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
bro = webdriver.Chrome(chrome_options=chrome_options, executable_path='./chromedriver.exe')
bro.get('https://www.baidu.com/')
print(bro.page_source)
bro.close()
5.2 edge
from selenium import webdriver
caps = {"browserName": "MicrosoftEdge",
"version": "",
"platform": "WINDOWS",
"ms:edgeOptions": {
'extensions': [],
'args': ['--headless',
'--disable-gpu']}}
driver = webdriver.Edge(executable_path='./msedgedriver.exe', capabilities=caps)
driver.get('https://www.baidu.com')
page_text = driver.page_source
print(page_text)
driver.close()
6 元素交互
tag.send_keys() # 往里面写内容
tag.click() # 点击控件
tag.clear() # 清空控件内容
bro.execute_script('window.open()') # 执行js代码
bor.back() # 浏览器后退
bor.forward() # 浏览器前进
bro.get_cookies() # 获取cookie
print(browser.window_handles) # 获取所有的选项卡
browser.switch_to_window(browser.window_handles[1]) # 切换选项卡
7 动作链
7.1 用法
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
action = ActionChains(browser)
username = browser.find_element(By.XPATH, '//*[@id="username"]')
password = browser.find_element(By.XPATH, '//*[@id="password"]')
login_btn = browser.find_element(By.XPATH, '//*[@id="login"]')
usr = 'username'
pwd = 'password'
# 链式写法
action.click(username).send_keys(usr).click(password).send_keys(pwd).click(login_btn).perform()
7.2 方法列表
click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素

浙公网安备 33010602011771号