js最小化浏览器_2.2.4 Selenium操作浏览器机制

浏览器基本操作

1.浏览器前进操作:

  • forward() 在初始操作时,是不存在前进操作,一般与back配合使用;

  • back后退操作(当前对象必须存在上下文)

2.浏览器的最大化、最小化、全屏:

  • get_driver.maximize_window()

  • get_driver.minimize_window()

  • get_driver.fullscreen_window()

3.浏览器close和quit两个方法的区别:

  • close表示的是关闭当前对象所处页面(操作页面)窗口,quit表示关闭所有的页面窗口并执行关闭驱动器;如果只存在一个窗口其产生的效果是相同的;

4.浏览器的相关属性获取:

  • print(get_driver.current_url)                      获取当前对象的url地址

  • print(get_driver.current_window_handle) 获取当前对象的句柄

  • print(get_driver.title)                                 获取当前对象的标题

  • print(get_driver.window_handles)           获取当前对象的所有句柄(选项卡)

WebElement接口常用方法
方法描述
clear清除元素的内容
send_keys在元素上模拟按键输入
click单击元素
submit提交表单
size返回元素的尺寸
text获取元素的文本
get_attribute(name)获得属性值
is_displayed()设置该元素是否用户可见
浏览器句柄操作
  • 驱动器对象.switch_to.window(句柄名)

  • 驱动器对象.switch_to_window(句柄名):该种方法属于保留方法,不建议使用,在后期会进行删除;句柄名可以通过获取所有句柄然后定义其索引;

建议:如果存在多个句柄的话,建议对每个句柄进行命名操作;便于句柄之间的切换
浏览器中的alert框处理
  • alert框是无法进行直接定位元素;

  • 需要使用switch_to.alert,先进行切换到alert对象中;

  • 然后进行调用对应的方法执行(accept()确定、dismiss()取消),同样还可以获取其文本内容,调用text属性即可;

浏览器的滚动条操作

浏览器滚动条是无法直接进行定位,所以也需要借助js脚本完成操作;
1.指定上下滚动的高度;

  • 此种浏览器滚动条可能会因为浏览器的类型不同版本不同环境不同可能执行失败

 js="var browser=document.documentElement.scrollTop=100"
get_driver.execute_script(js)

2.实现左右滚动还可以实现上下滚动;

  • 调用的脚本是window.scrollTo(x,y) 其中x表示的是横向滚动,y表示的是纵向滚动;表示的是相对原点进行滚动;

get_driver.execute_script("window.scrollTo(0,100)")
  • window.scrollBy(x,y):表示的是相对当前的坐标点进行再次滚动指定的x,y坐标的值;

3.不考虑其横向滚动和纵向滚动的坐标值,直接滚动到指定元素位置;

  • 使用的脚本语法:

arguments[0].scrollIntoView();

其中arguments[0]表示的是传入的定位元素的对象,在执行execute_script方法时传入的第二个参数;等价于直接使用js脚本的元素定位:

document.getElementById('id的值').scrollIntoView();

然后scrollIntoView方法中默认值是true,表示的是滚动到的元素作为第一行进行显示;同样可以设置false值,如果是false值则表示指定元素的作为最后一行进行显示;

综合实例

#-*- coding:utf-8 -*-##-------------------------------------------------------------------------#ProjectName:       Python2020#FileName:          BrowserTest.py#Author:            mutou#Date:              2020/6/11 21:58#Description:需要:点击退货流程#--------------------------------------------------------------------------from Day18.BaseModule.Base_Class import BaseClassimport timeclass  Browser_Test(BaseClass):    def __init__(self,url):        super().__init__(url)    #点击退货    def  click_tui_huo(self):        # self.get_driver.maximize_window()        print(self.get_driver.get_window_size())       # self.get_driver.execute_script("document.documentElement.scrollTop=2000;")        time.sleep(2)        self.get_driver.execute_script("$('.close-fixed-suspension').click()")        # self.get_driver.execute_script("window.scrollTo(1000,1000);")        # self.get_driver.execute_script("window.scrollBy(0,1000);")        get_element=self.get_driver.find_element_by_link_text("退换货政策")        #思考,直接通过js完成        time.sleep(3)        self.get_driver.execute_script("javascript:alert('确定吗?');")        #alert框无法定位        time.sleep(4)        get_alert=self.get_driver.switch_to.alert        #其核心操作无非就是dismiss(取消)和accept(接受)        get_alert.accept()        self.get_driver.execute_script("arguments[0].scrollIntoView()",get_element)        time.sleep(2)        get_element.click()if __name__ == '__main__':    bo=Browser_Test("http://123.57.71.195:8787/index.php/home")    bo.click_tui_huo()
下拉框处理(Select模块)
  • 在UI自动化测试过程中,经常会遇到一些下拉框,如果我们基于Webdriver操作的话就需要click两次,而且很容易出现问题,实际上Selenium给我们提供了专门的Select(下拉框处理模块)。

  • 有多种方法可以对下拉框中的元素进行选择

下面代码操作系统界面:

90e41dda4c72cccd482a0ffaa5c894bb.png

一、间接选择
  • 先定位到下拉框,再定位其中的选项

  • 代码:

#-*- coding:utf-8 -*-##-------------------------------------------------------------------------#ProjectName:       Python2020#FileName:          IframeTest.py#Author:            mutou#Date:              2020/6/11 21:09#Description:iframe的切换#--------------------------------------------------------------------------from Day18.BaseModule.Login_Class import LoginClassimport timeclass AddCustom(LoginClass):    def __init__(self,url,username,password):        super(AddCustom, self).__init__(url)        self.username=username        self.password=password    #完成添加客户的点击操作;前置条件:是出于登录状态的    def  click_add_customer(self):        self.login(self.username, (self.password))        #点击客户管理        click_first = "$('li:first').click()"        self.get_driver.execute_script(click_first)        time.sleep(4)        #切换iframe        #self.get_driver.switch_to.frame("Open11")        #没有id/没有name,可以传入定位iframe的对象,切换到客户管理页面        self.get_driver.switch_to.frame(self.get_driver.find_element_by_xpath("//div[@class='aui_content aui_state_full']/iframe"))        self.get_driver.find_element_by_xpath("//a[@data-]").click()        time.sleep(2)        #切回主文档        self.get_driver.switch_to.default_content()        #切换iframe,切到新增客户页面        self.get_driver.switch_to.frame("OpenOpen11")        time.sleep(2)        #先定位到select对象        get_select=self.get_driver.find_element_by_xpath("//*[@id='form1']/div[1]/dl[3]/dd/select")        #再定位option        get_select.find_element_by_css_selector("option[value='A客户']").click()if __name__ == '__main__':    add=AddCustom("http://123.57.71.195:7878/","admin","admin888")    add.click_add_customer()
二、直接选择
  • 直接定位到下拉框中的选项

  • 代码:间接选择的代码块中第35行和第37行代码可以通过以下一行代码表示

self.get_driver.find_element_by_xpath("//*[@id='form1']/div[1]/dl[3]/dd/select/option[2]").click()
三、Select模块

webdriver提供了一个Select模块来专门对下拉框进行处理,使用时需要导入Select模块 

from selenium.webdriver.support.select import Select

其中有三种定位选项的方法:

  • select_by_index():索引定位(从0开始)

  • select_by_value():value属性定位

  • select_by_visible_text():选项的文本属性

代码:

#-*- coding:utf-8 -*-##-------------------------------------------------------------------------#ProjectName:       Python2020#FileName:          IframeTest.py#Author:            mutou#Date:              2020/6/11 21:09#Description:iframe的切换#--------------------------------------------------------------------------from Day18.BaseModule.Login_Class import LoginClassimport timefrom selenium.webdriver.support.select import Selectclass AddCustom(LoginClass):    def __init__(self,url,username,password):        super(AddCustom, self).__init__(url)        self.username=username        self.password=password    #完成添加客户的点击操作;前置条件:是出于登录状态的    def  click_add_customer(self):        self.login(self.username, (self.password))        #点击客户管理        click_first = "$('li:first').click()"        self.get_driver.execute_script(click_first)        time.sleep(4)        #切换iframe        #self.get_driver.switch_to.frame("Open11")        #没有id/没有name,可以传入定位iframe的对象,切换到客户管理页面        self.get_driver.switch_to.frame(self.get_driver.find_element_by_xpath("//div[@class='aui_content aui_state_full']/iframe"))        self.get_driver.find_element_by_xpath("//a[@data-]").click()        time.sleep(2)        #切回主文档        self.get_driver.switch_to.default_content()        #切换iframe,切到新增客户页面        self.get_driver.switch_to.frame("OpenOpen11")        time.sleep(2)        #获取Select对象        selecct_object=Select(self.get_driver.find_element_by_xpath("//*[@id='form1']/div[1]/dl[3]/dd/select"))        #通过索引获取        selecct_object.select_by_index(2)        #通过value值获取        selecct_object.select_by_value("D客户")        #通过文本值获取        selecct_object.select_by_visible_text("B客户")if __name__ == '__main__':    add=AddCustom("http://123.57.71.195:7878/","admin","admin888")    add.click_add_customer()

Select提供了四种取消选中项的方法

deselect_all             # 取消全部的已选择项deselect_by_index        # 取消已选中的索引项deselect_by_value        # 取消已选中的value值deselect_by_visible_text # 取消已选中的文本值

注意事项:

在日常的web测试中,会经常遇到某些下拉框选项已经被默认选中,这种时候就需要用到这里所说的四种方法;

了解更多信息关注公众号

39279aefa5928afdf7abed529dcc0eb6.png

b44dd7cb6c3cd6e157185721b1619800.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值