selenium 常用操作总结

2023-11-18

谷歌驱动下载

http://chromedriver.storage.googleapis.com/index.html

参数设置options
opt = Options()
opt.add_argument('--headless')
# 无头模式
opt.add_argument('--no-sandbox')
opt.add_argument('--disable-dev-shm-usage')

# 取消沙盒模式,linux配置时需要
opt.add_argument('--disable-gpu')
opt.add_argument('--start-maximized')
# 初始界面最大化,相当于max_size
opt.add_argument('--incognito')
# 隐身模式
opt.add_argument(f'user_agent="{Factory.create().user_agent()}"')
# 配置ua
opt.add_argument('--disable-blink-features=AutomationControlled')
# 规避检测
opt.add_experimental_option('useAutomationExtension', False)
opt.add_experimental_option("excludeSwitches", ['enable-automation'])
# 取消自动化测试显示的字样
proxy = '220.191.64.149'
chrome_options.add_argument('--proxy-server = http://' + proxy)
# 代理
driver = Chrome(options=opt)

界面刷新
driver.refresh()
滚动条的使用
from selenium import webdriver 滚动条的使用
chrome = webdriver.Chrome()
chrome.get(url)

js='document.documentElement.scrollTop=10000' # 最大值测试
chrome.execute_script(js)
chrome.implicitly_wait(2)
chrome.refresh()

driver.execute_script('window.scrollBy(0,1000)')
#  scrollBy(x,y)中,x为必须参数,表示向右滚动的像素值;y也为必须参数,表示向下滚动的像素值

for y in range(30):
       js='window.scrollBy(0,100)'
       driver.execute_script(js)
       time.sleep(0.5)
# 慢慢滚动
窗口最大化代码
spider.chrome.maximize_window()
转换窗口
chrome.switch_to.window(chrome.window_handles[-1])
# 转换到最后一个窗口
chrome.switch_to.window(chrome.window_handles[0])
# 转换到第一个窗口

chrome.current_window_handle
# 获取当前窗口
无头和防屏蔽设置
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options'

options = Options()

options.add_argument('--headless')
options.add_argument('--disable-gpu') 
# 无头浏览器设置

opt.add_argument('--disable-blink-features=AutomationControlled')
# 规避无头检测的方法

chrome=Chrome(options=options)
选择器的使用
from selenium.webdriver.support.select import Select

el_el = chrome.find_element_by_xpath('//')
# 先找到选择器对应的xpath
sel = Select(sel_el)

for i in range(len(sel.options)): 和设置无头的Options不一样 大小写区分
	sel.select_by_index(i)
	# 通过索引进行检索
	time.sleep(2)
输入字符,点击回车
from seleium.webdriver.common.keys import Keys


chrome.find_element_by_xpath('*').click
# 点击xpath

chrome.find_element_by_xpath('*').send_keys('字符串', Keys.ENTER)
sleep(1)
# 输入字符串,并且点击回车

处理frame框架
# selenium 中遇到 iframe 框架时 想要提取iframe中的标签

chrome.switch_to.frame(xpath(*))  
# 先转换到frame框架里面
chrome.switch_to.default.content()
# 再切回来

处理弹窗(还未实践待验证)
# 遇到弹窗,关闭或者进入
alert_el = chrome.find_element_by_xpath('*')

chrome.switch_to.alert(alert_el)
# 转换到弹窗
chrome.dismiss()
# 关闭弹窗
chrome.accept()
# 进入弹窗


动作链的使用
from selenium.webdriver.common.action_chains import ActionChains

ActionChains(chrome).move_to_element_with_offset(code_el, x, y).click().perform()
# code_el 表示的是验证码坐标,x,y 表示坐标的值(整数)  一定要执行perform后才能运行

ActionChains(chrome).click_and_hold(element).perform()
# 点击鼠标左键,按住不放

release(on_element=None)
# 在某个元素位置松开鼠标左键

move_by_offset(xoffset, yoffset)
# 将鼠标移动到当前鼠标位置的偏移位置

move_to_element_with_offset(to_element, xoffset, yoffset)
# 将鼠标移动到距某个元素多少距离的位置

drag_and_drop(source, target)
# 拖拽到某个元素然后松开

drag_and_drop_by_offset(source, xoffset, yoffset)
# 拖拽到某个坐标然后松开

double_click(on_element=None)
# 双击鼠标左键

context_click(on_element=None)
# 单击鼠标右键

关闭窗口和关闭浏览器
chrome.close()
# 关闭当前窗口

chrome.quit()
# 关闭整个浏览器
隐性等待并且刷新页面
chrome.implicitly_wait(2)
chrome.refresh()

# 一般用于执行拉屏操作后
键盘各种键的操作
from selenium.webdriver.common.keys import Keys

send_keys(Keys.BACKSPACE)
# 删除键

send_keys(Keys.SPACE)
#空格键

send_keys(Keys.TAB)
# 制表键

send_keys(Keys.ESCAPE)
# 回退键

send_keys(Keys.ENTER)
# 回车键

send_keys(Keys.CONTROL,'a')
# 全选‘a’,复制‘c’,剪切‘x’,黏贴‘v’

send-keys(Keys.F1)
# 键盘 F1  ~  F12

截图
el_el = chrome.find_element_by_xpath('*')
el_el.get_screenshot_as_file('****.png')
# 保存为图片
el_el.get_screenshot_as_png()
# 保存为二进制

参考链接: https://www.cnblogs.com/jiyu-hlzy/p/12158978.html#example4

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

selenium 常用操作总结 的相关文章