python使用selenium模拟人工操作

概述

有时候业务中的一些固定流程的测试环境需要重复执行很多次;这种场景其实可以用pythonselenium库模拟用户手动点击输入,实现自动化测试;

我这边的python版本是
Python 3.6.7rc2

demo

# coding=utf-8
import time
import logging
import re
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


def login(driver):

    url = "http://192.168.xxx.xxx/xxx/xxx/add"

    # 打开网页
    driver.get(url)  # 打开url网页 比如 driver.get("http://www.baidu.com")

    login_user_name_xpath = '//*[@id="userName"]'
    login_user_passwd_xpath = '//*[@id="password"]'

    # 等待登录账户输入框dom加载
    wait_xpath_dom_loading(driver, login_user_name_xpath)
    wait_xpath_dom_loading(driver, login_user_passwd_xpath)

    # 登录自动获得token
    driver.find_element_by_xpath(login_user_name_xpath).send_keys('xxx')
    driver.find_element_by_xpath(login_user_passwd_xpath).send_keys('xxx')
    driver.find_element_by_xpath('//*[@id="root"]/div/div/div/div[2]/form/button').click()

    # 等待登录后的网页跳转
    time.sleep(2)

    return driver

def memberFillDataAndSubmit_01(driver):

    url = "http://xxx.xxx.xxx.xxx/xxx/xxx/add"

    # 打开网页
    driver.get(url)

    # 等待页面加载
    time.sleep(2)

    try:
        driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/ul')
    except:
        print('未找到分页元素,当前页面已无数据')
        return driver

    # 填充数据
    driver.find_element_by_xpath(
        '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[2]/div[2]/div/div[2]/input').send_keys("2")
    driver.find_element_by_xpath(
        '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[3]/div[2]/div/div[2]/input').send_keys("3.5")

    for count in range(0,170):

        # 页面一件填充按钮
        driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/button').click()

        # 数据全选按钮
        driver.find_element_by_xpath(
            '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label/span/input').click()

        # 数据提交
        driver.find_element_by_xpath(
            '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[2]/div[2]/div[3]/button').click()

        # 等待页面加载
        time.sleep(2)

    return driver

def memberFillDataAndSubmit_02(driver):

    url = "http://xxx.xxx.xxx.xxx/xxx/xxx/add"

    # 打开网页
    driver.get(url)

    # 等待页面加载
    time.sleep(2)

    try:
        driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/ul')
    except:
        print('未找到分页元素,当前页面已无数据')
        return driver

    # CA填报
    driver.find_element_by_xpath('//*[@id="advanced_search"]/div/div[1]/div[2]').click()
    # 等待页面加载
    time.sleep(2)

    # 填充数据
    driver.find_element_by_xpath(
        '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[2]/div[2]/div/div[2]/input').send_keys("2")
    driver.find_element_by_xpath(
        '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[3]/div[2]/div/div[2]/input').send_keys("3.5")

    for c in range(0,24):
        # 页面一件填充按钮
        driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/button').click()

        # 数据全选按钮
        driver.find_element_by_xpath(
            '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div').click()

        # 数据提交
        driver.find_element_by_xpath(
            '//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[2]/div[2]/div[3]/button').click()

        # 等待页面加载
        time.sleep(2)

    return driver


def groupDataCheckAndSubmitWrapper(driver):
    url = "http://xxx.xxx.xxx.xxx/xxx/xxx/manager"

    # 状态下拉框xpath
    status_optional_xpath = '//*[@id="advanced_search"]/div/div[5]/div/div/div[2]/div/div/div'

    # 当前页面数据全选
    current_page_total_choice_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[3]/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label'

    # 状态下拉框中option xpath
    status_optional_wait_check_xpath = '/html/body/div[2]/div/div/div/div[2]/div/div/div/div[3]'

    # 查询按钮xpath
    query_btn_xpath = '//*[@id="advanced_search"]/div/div[19]/div/div[2]/button'


    # 打开网页
    driver.get(url)
    time.sleep(5)

    # 等待数据全选渲染完毕(数据请求完毕)
    wait_xpath_dom_loading(driver, current_page_total_choice_xpath)

    # 创建ActionChains对象
    actions = ActionChains(driver)

    # 点击唤出状态下拉框
    status_optional = driver.find_element_by_xpath(status_optional_xpath)
    actions.move_to_element(status_optional).perform()
    status_optional.click()
    time.sleep(1)

    # 点击待审核状态
    click_delay_time(driver, status_optional_wait_check_xpath)

    # 重新查询
    click_delay_time(driver, query_btn_xpath)
    time.sleep(3)

    submit_type_xpath_list = [
        # 标签页2
        '//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[2]/label[2]',
        # 标签页3
        '//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[3]/label[2]',
        # 标签页4
        '//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[4]/label[2]'
    ]
    # 首次进入页面,查询标签页1数据
    groupDataCheckAndSubmit(driver, actions)

    for submit_type_xpath in submit_type_xpath_list:
        driver.find_element_by_xpath(submit_type_xpath).click()
        time.sleep(10)

        # 数据审核提交
        groupDataCheckAndSubmit(driver, actions)



def groupDataCheckAndSubmit(driver, actions):

    # 状态筛选下拉浮窗hover位置
    check_submit_hover_position_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[2]/div[2]/div[1]/div/div[2]/button'
    # 浮窗中 通过并提交 按钮xpath
    pass_and_submit_xpath = '/html/body/div[3]/div/div/ul/li[2]/span/a'

    # 当前页面数据全选
    current_page_total_choice_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[3]/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label'

    # 审核按钮hover位置dom加载
    wait_xpath_dom_loading(driver, check_submit_hover_position_xpath)

    # 审核按钮hover位置
    check_submit_hover_position = driver.find_element_by_xpath(check_submit_hover_position_xpath)

    try:
        # 通过class获取分页li
        total_page_li_list = driver.find_element_by_class_name('ant-pagination').find_elements_by_tag_name('li')

        # 过滤获取页码对应xpath对象
        page_li_list = list(
            filter(get_max_page_num, total_page_li_list)
        )

        total_page_num = int(page_li_list[len(page_li_list) - 1].get_attribute('title'))

        # 没有数据
        if 0 == total_page_num: return driver

    except Exception as e:
        # 没有数据时当前页面没有渲染分页下标
        logging.exception(e)
        return driver


    for count in range(0,total_page_num):
        # 提交后页面重新加载
        time.sleep(5)

        # 数据全选
        wait_xpath_dom_loading(driver, current_page_total_choice_xpath)
        click_delay_time(driver, current_page_total_choice_xpath)

        # 进行重置坐标
        actions.reset_actions()

        # 鼠标悬停,唤出按钮
        actions.move_to_element(check_submit_hover_position).perform()
        # js加载
        time.sleep(2)


        # 鼠标hover到审核通过按钮上.ran后点击按钮
        check_and_submit_btn = driver.find_element_by_xpath(pass_and_submit_xpath)
        # actions.move_to_element(check_and_submit_btn).perform()
        # js加载
        #time.sleep(1)
        # 通过并提交
        check_and_submit_btn.click()


    return driver


# WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
# driver: 浏览器驱动
# timeout: 最长超时时间
# poll_frequency: 检测间隔时间,默认0.5s
# ignored_exceptions: 超时后的异常信息,默认情况抛出NoSuchElementException异常
# 一般由until()或until_not方法配合使用,下面是这两种方法的说明:
# until(method, message=''): 调用该方法提供的驱动程序作为一个参数,直到返回值为True;
# until_not(method, message=''): 调用该方法提供的驱动程序作为一个参数,直到返回值为Flase;
def wait_xpath_dom_loading(driver, xpath):
    locator = (By.XPATH, xpath)
    WebDriverWait(driver, 10, 1).until(EC.presence_of_element_located(locator))

def click_delay_time(driver, xpath):
    driver.find_element_by_xpath(xpath).click()
    time.sleep(2)

def get_max_page_num(xpath_obj):
    return re.match('\d', xpath_obj.get_attribute('title'))

if __name__ == '__main__':

    driver_path = 'D:\chromeDownLoad\chromedriver-win64\chromedriver.exe'

    driver = webdriver.Chrome(driver_path)  # Chrome浏览器

    # 设置全局等待超时时间5s
    driver.implicitly_wait(5)

    try:
        # 登录
        driver = login(driver)

        # 数据填报提交
        memberFillDataAndSubmit_01(driver)

        # 数据填报提交
        memberFillDataAndSubmit_02(driver)

        # 组长数据审核提交
        groupDataCheckAndSubmitWrapper(driver)

    except Exception as e:
        logging.exception(e)

    # 关闭浏览器
    driver.quit()
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值