Web自动化测试示例

1、需求

统计最近一周特定城市数据抓取是否缺失

2、使用包

1、pip install selenium

2、根据本地chrome版本,下载对应chrome驱动,放到当前目录
http://npm.taobao.org/mirrors/chromedriver/

当前使用版本 
http://npm.taobao.org/mirrors/chromedriver/81.0.4044.69/
chromedriver_win32.zip   

3、示例代码

数据界面
import datetime #精确时间
from selenium import webdriver

driver = webdriver.Chrome('chromedriver.exe')
#全局等待,隐性等待
driver.implicitly_wait(10)


# 默认显示第一个菜单数据面板,可不用点击菜单
def location_element():
    # 元素多 class时,查找包含
    eles = driver.find_elements_by_css_selector('div[class~=dashboard-stat]')

    for ele in eles:
        # 直接使用 text 会导致可见区域以外输出为空
        # print(ele.find_element_by_css_selector('div[class=desc]').text)
        # print(ele.find_element_by_css_selector('div[class=desc]').get_attribute('innerText'))
        if ele.find_element_by_css_selector('div[class=desc]').get_attribute('innerText') == 'xxxxxxxxxxxxx':
            # 直接查找 a 标签点击,提示出错,element not interactable
            # ele.find_element_by_css_selector('a[class=more]').click()
            # 修改为先滚动定位至该位置,再点击
            ele_a = ele.find_element_by_css_selector('a[class=more]')
            driver.execute_script("arguments[0].scrollIntoView();", ele_a)
            ele_a.click()

            # 循环比较最近一周抓取数据
            for i in range(-7,0):
                now = datetime.datetime.now()
                start_date = (datetime.datetime.now() + datetime.timedelta(days=i)).strftime('%Y-%m-%d 00:00:00')
                end_date = (datetime.datetime.now() + datetime.timedelta(days=i+1)).strftime('%Y-%m-%d 00:00:00')
                check_app_data(start_date,end_date)


# 展开指定页面后,过滤、查询、比对输出
def check_app_data(start_date,end_date):
    # 选择城市
    city = driver.find_element_by_id('txt_Space0046A.column_3')
    city.clear()
    city.send_keys('北京市')

    # 设置开始时间、结束时间
    start = driver.find_element_by_id('txt_Space0046A.column_14')
    start.clear()
    start.send_keys(start_date)

    end = driver.find_element_by_id('txt_Space0046A.column_14_End')
    end.clear()
    end.send_keys(end_date)

    driver.find_element_by_link_text(u"查询").click()

    text = driver.find_element_by_css_selector(".pagination.pull-right").get_attribute('innerText')
    print(datetime.datetime.strptime(start_date,'%Y-%m-%d %H:%M:%S').date(),text.split("/")[1])


def login():
    driver.get('http://localhost:29150/Theme_M/login.aspx')
    # 输入用户名
    driver.find_element_by_id('username').send_keys('test')

    # 输入密码
    driver.find_element_by_id('password').send_keys('123456')

    # 登录
    driver.find_element_by_tag_name('button').click()

    location_element()

login()

结果输出

你可能感兴趣的:(Web自动化测试示例)