原文地址:https://blog.csdn.net/xiaoxiaochunmei/article/details/89226424
调用chromeOptions
from selenium import webdriver options = webdriver.ChromeOptions() ...... browser = webdriver.Chrome(chrome_options=option)
去掉提示“正在受到自动测试软件的控制”
option.add_argument('disable-infobars')
设置窗口大小
option.add_argument('--window-size=1366,768')
或者
browser.set_window_size(480, 800) #移动端大小
最大化运行
browser.maximize_window()
or
option.add_argument('--start-maximized')
设置编码格式
option.add_argument('lang=zh_CN.UTF-8')
设置UA请求头
option.add_argument('User-Agent=Mozilla/5.0')
无界面浏览器
option.add_argument('--headless')
禁止加载图片
my_prefs = {"profile.managed_default_cotent_settings.images": 2}
option.add_experimental_option("prefs", my_prefs)
IP代理
# 设置无账号密码的代理
option.add_argument('--proxy-server=http://ip:port')
# 设置有账号密码的代理,需要其他插件
关闭是否保存密码的提示框
prefs = {}
prefs['credentials_enable_service'] = False
prefs['profile.password_manager_enabled'] = False
option.add_experimental_option("prefs", prefs)
指定浏览器的分辨率
option.add_argument('window-size=1920 × 3000')
或者
browser.set_window_size(480, 480)
手动指定浏览器的位置
option.binary_location = r'绝对路径chrome.exe'
浏览器
# 浏览器刷新 browser.refresh() # 浏览器后退 browser.back() # 浏览器前进 browser.forward()
拖动滑动条
js = "var q=document.documentElement.scrollTop=1000000"
or
js = "window.scrollTo(0,document.body.scrollHeight)" # window.scrollTo(x,y)
browser.execute_script(js)
or
browser.execute_script("window.scrollTo(document.body.scrollHeight,0)")
or
#获取焦点
target = browser.find_element_by_class_name('top-div')
b.execute_script("arguments[0].focus();", target)
or
#拖动到可见的元素去
target = browser.find_element_by_id('1')
browser.execute_script("arguments[0].scrollIntoView();", target)
frame标签
# frame标签有三种:frameset、frame、iframe。其中frameset是普通标签,无需切换;frame需要层层切进去。 1. 切换到frame中:switch_to.frame(fererence) 参数可以是:id、name、index以及webElement对象 2.切换回主文档:switch_to.default_content() 嵌套frame的操作:switch_to.parent_frame() 从子frame切换到父frame
多窗口切换
# WebDriver提供switch_to.window()方法实现不同窗口间的切换 1. current_window_handle:获得当前窗口句柄 2. window_handles:返回所有窗口的句柄到当前会话 3. switch_to.window():切换到相应的窗口 # 获得当前所有打开的窗口句柄 all_handles = browser.window_handles # 获得当前窗口句柄 curr_window = browser.current_window_handle
告警框处理
# JS生成的告警框类型有:alert、confirm、prompt。处理告警框首先使用switch_to.alert()方法定位,然后使用text、accept、dismiss、send_keys等进行操作。 text:返回告警框中的文字信息 accept():接受现有告警框 dismiss():解散现有告警框 send_keys():在告警框中输入文本
执行js
# 将不可见元素改为可见的
from selenium.webdriver.common.action_chains import ActionChains as ac
# 执行js修改属性
js = "document.getElementById('licensemg').style.display='block'"
browser.execute_script(js)
# 将光标悬停在元素上进行下一步的操作
ele = browser.find_element_by_id('licensemenu')
ac(browser).move_to_element(ele).perform()
下拉菜单
select有两种模式:单选和多选,功能有:取消选中option、选中option、判断是否是多选模式、获取option。
其中,取消选中仅对多选模式有效;其他方法单选多选均有效。
- 定位下拉菜单控件
from selenium.webdriver.support.select import Select # 定位下拉菜单的控件位置 div_select = Select(browser.find_element_by_id('select_id')) - 选中option
#通过文本值定位 div_select.select_by_visible_text('option_text') # 通过索引定位(索引从0开始) div_select.select_by_index(index) # 通过value值定位 div_select.select_by_value(select_value) - 取消选中option
仅对多选模式有效,否则报异常# 取消所有选择项 div_select.deselect_all() # 取消指定index的选择 div_select.deselect_by_index(index) # 取消指定value值的选择 div_select.deselect_by_value(value) # 取消指定文字项的选择(单选模式无效,但不会报异常) div_select.deselect_by_visible_text(text)
- 判断是否是多选
返回booleandiv_select.is_multiple()
- 获取option
# 返回所有的option(没有option时,会返回空列表,不报异常) div_select.options() # 返回所有已选中的option(没有option被选中,会返回空列表,不报异常) div_select.all_selected_options() # 返回选中的第一个option(没有option被选中,会报异常) div_select.first_selected_option()
单选按钮和复选框
一般情况下,这两种标签都是input标签,可以通过点击的方式进行选中。 可按照一般元素进行定位。 Checkbox: <input type=“checkbox” value=“cv1” name=“c1”> <input type=“checkbox” value=“cv2”> Radio: <input type=“radio” value=“rv1” name=“r1”> <input type=“radio” value=“rv2” name=“r1”> 判断是否被选中: div.is_selected(),返回boolean
来源:https://www.cnblogs.com/wtil/p/12286800.html