美文网首页
python3+selenium_UI自动化_异常情况处理

python3+selenium_UI自动化_异常情况处理

作者: 雷阳洪 | 来源:发表于2019-08-27 18:18 被阅读0次

    校验执行的结果是否与预期的结果一致

    # 校验训练是否成功
            # 获取界面元素,存起来
            nums = driver.find_elements_by_xpath('(//tr[@class="el-table__row"])[1]/td[5]')
            # 声明一个boolean变量
            b = False
            # n.text取出元素的可视文本
            for n in nums:         
                print(n.text)
                # 如果相同,就将true赋值给b
                if n.text == 'SUCCESS':
                    b = True
            # 断言,是否正确
            assert True == b
    

    selenium 处理select下拉框

    from selenium import webdriver
    import time
    from selenium.webdriver.support.select import Select
    
    
    def test_select1(driver):
        driver.get("http://ui.yansl.com/#/select")
        time.sleep(1)
        s = driver.find_element_by_xpath('//select[@name="item1"]')
        # 导入select方法
        select = Select(s)
        # 通过select选项的索引来定位选择对应选项(从0开始计数),如选择第三个选项:select_by_index(2)
        select.select_by_index(1)
        time.sleep(1)
        # 通过 value来选择对应选项
        select.select_by_value('huawei')
        time.sleep(1)
        # 通过text来选择对应选项
        select.select_by_visible_text('小米')
        time.sleep(1)
    

    模拟鼠标键盘操作

    #  导入ActionChains 类
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    def test_select6(driver):
        driver.get("http://ui.yansl.com/#/select")
        time.sleep(1)
        s = driver.find_element_by_xpath("//input[@name='item6']")
        # s.click()
        # time.sleep(1)
        s.send_keys('双')
        time.sleep(1)
        # 实例化ActionChains
        actions=ActionChains(driver) 
        # 模拟键盘操作ENTER键,按住ENTER,松开ENTER.
        actions.key_down(Keys.ENTER).key_up(Keys.ENTER).perform()
        time.sleep(1)
    

    显性等待时间,规定时间内循环查看指定元素

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    
    # 断言等待时间
    def test_wait_time(driver):
        driver.get("http://ui.yansl.com/#/messageBox")
        driver.find_element_by_xpath(
            "(//div[@class='el-submenu__title'])[3]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//li[contains(text(),'等待加载(2)')]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//span[contains(text(),'指令方式')]").click()
        xpath = "//div[contains(text(),'王小虎')]"
        # 显示等待,20秒内每0.5秒去查看指定的元素是否加载出来
        WebDriverWait(driver, 20, 0.5).until(
            expected_conditions.presence_of_element_located((By.XPATH, xpath))
        )
        ele = driver.find_element_by_xpath("//div[contains(text(),'王小虎')]")
        # 断言元素是否存在
        assert ele.is_displayed()
        time.sleep(2)
    

    显示等待,节点不可见

    def test_demo1(driver):
        base = baseUI(driver)
        # 打开网址
        driver.get("http://buickview.saic-gm.com")
        # 输入用户名
        base.send_keys("输入用户名", "//input[@type='text']", "pr3foj")
        # 输入密码
        base.send_keys("输入密码", "//input[@type='password']", "19940102Dyj")
        # 点击登录
        base.click('点击登录', "//button[@id='J_sidebar_login']")
        base.click('点击忽略', "//button[@id='ignore']")
        time.sleep(1)
    
        xpath = '(//div[@class="el-loading-mask"])[1]'
        # 显示等待,节点不可见所使用的等待方法为"invisibility_of_element_located"
        WebDriverWait(driver, 20, 0.5).until(
            expected_conditions.invisibility_of_element_located((By.XPATH, xpath))
        )
        # 滚动窗口
        # js = "var q=document.documentElement.scrollTop=10000"  # 滚动窗口,滚到底  10000代表滚动的长度
        # driver.execute_script(js)
        # time.sleep(2)
        # base.click("删除默认大区","//i[@class='el-input__icon el-icon-arrow-down']")
    
        # 获取经销商库存的(今日)总值
        a = driver.find_element_by_xpath("(//div[@class='detail-number-content'])[last()]/div/span").text
        print(a)
    

    上传文件操作

    import autoit
    
    # auto插件上传文件
    def test_auto_upload_files(driver):
        driver.get("http://ui.yansl.com/#/messageBox")
        driver.find_element_by_xpath(
            "(//div[@class='el-submenu__title'])[1]").click()
        time.sleep(1)
        driver.find_element_by_xpath("// li[contains(text(), '上传文件(6)')]").click()
        time.sleep(1)
        driver.find_element_by_xpath(
            "(//span[contains(text(), '点击上传')])[2]").click()
        time.sleep(1)
        autoit.control_set_text("打开", "Edit1", 'D:\\leiyh.jpg')
        time.sleep(1)
        autoit.control_click("打开", "Button1")
        time.sleep(1)
    

    滚动窗口

    # 滚动下拉,滚动到懒加载行
    def test_scroll_down(driver):
        driver.get("http://ui.yansl.com/#/messageBox")
        driver.find_element_by_xpath(
            "(//div[@class='el-submenu__title'])[2]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//li[contains(text(), '表格(9)')]").click()
        time.sleep(1)
        js = "var q=document.documentElement.scrollTop=10000"  # 滚动窗口,滚到底  10000代表滚动的长度
        driver.execute_script(js)
        time.sleep(1)
    

    拖动滑块

    from selenium.webdriver import ActionChains
    
    
    # 拖动滑块
    def test_drag_slider(driver):
        # 拖动横向坐标
        driver.get("http://ui.yansl.com/#/messageBox")
        driver.find_element_by_xpath(
            "(//div[@class='el-submenu__title'])[1]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//li[contains(text(), '滑块(5)')]").click()
        time.sleep(1)
        # 获取横向滑块元素
        slider1 = driver.find_element_by_xpath(
            "(//div[@class='el-tooltip el-slider__button'])[1]")
        time.sleep(1)
        # 获取元素的location属性
        location = slider1.location
        x = location['x']
        y = location['y']
        print(x)
        print(y)
        # actionchains按偏移量拖拽
        actions = ActionChains(driver)
        actions.drag_and_drop_by_offset(slider1, x + 10, y).perform()
    

    selenium循环遍历当前页面的title,判断预期值是否在title中

    def test_jd_demo6(driver):
        driver.get("file:///C:/Users/52826/Desktop/index.html")
        # 1.starts-with的解释是“和字面意思一样就是以某某开头” 例如://input[starts-with(@class,'xxx')]
        text1 = driver.find_element_by_xpath("(//a[starts-with(@href,'http://www.baidu.com')])[last()]")
        time.sleep(2)
        actions = ActionChains(driver)
        actions.key_down(Keys.CONTROL).click(text1).key_up(Keys.CONTROL).perform()
        time.sleep(2)
        # 获取页面的全部句柄
        handles = driver.window_handles
        # 循环遍历当前页面的句柄
        for handle in handles:
            time.sleep(2)
            # 循环跳转浏览器页面
            driver.switch_to_window(handle)
            # 获取页面的title
            title = driver.title
            # 判断预期值是否在title中, 满足就结束跳转,停留在当前页面
            if '新闻' in title:
                break
            time.sleep(2)
    

    selenium登录验证码拖动滑块操作

    from selenium.webdriver import ActionChains
    
    
    # 拖动滑块
    def test_drag_slider(driver):
        # 拖动横向坐标
        driver.get("http://ui.yansl.com/#/messageBox")
        driver.find_element_by_xpath(
            "(//div[@class='el-submenu__title'])[5]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//li[contains(text(),'直线滑动(1)')]").click()
        time.sleep(1)
        # 获取横向滑块元素
        slider1 = driver.find_element_by_xpath(
            "//*[@id='code-box']/span")
        time.sleep(1)
        # actionchains按偏移量拖拽
        actions = ActionChains(driver)
        actions.click_and_hold(slider1).perform()  # 鼠标左键按下不放
        actions.move_by_offset(298, 0)  # 需要滑动的坐标
        actions.release().perform()  # 释放鼠标
        time.sleep(1)
    

    相关文章

      网友评论

          本文标题:python3+selenium_UI自动化_异常情况处理

          本文链接:https://www.haomeiwen.com/subject/eprzfctx.html