Python Selenium 自动测试网页

自动测试脚本,多用于regression testing

  1. 需要安装Python 和 Selenium
    http://selenium-python.readthedocs.io/

  2. Python 用的IDE: AptanaStudio

  3. Selenium 默认浏览器是firefox, 如果用Chrome或者IE 需要自己下 web driver
    IE
    http://stackoverflow.com/questions/24925095/selenium-python-internet-explorer

  4. 如果用的是IE浏览器,输入速度非常慢。需要下面的代码优化一下

caps = DesiredCapabilities.INTERNETEXPLORER
caps['requireWindowFocus'] = True
driver_ie="C:\\Users\\yaqi.zhang\\Downloads\\\IEDriverServer_Win32_2.42.0\\IEDriverServer.exe";
driver=webdriver.Ie(driver_ie,capabilities=caps)
  1. 一般用来寻找tag的语句如下

我所用到的所有库

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

from selenium.common.exceptions import NoSuchElementException     

by_name

driver.find_element_by_name('btnOK').click()

by_id

driver.find_element_by_id('btnOK').click()

by_xpath

driver.find_element_by_xpath("//input[@value='Save']").click()

在table中选择

    tr=driver.find_element_by_xpath("//tbody[@id='ListBody']/tr[1]")
    td=tr.find_elements(By.TAG_NAME, "td")[2]

    if td.text =='':
        return     

通过css选元素

                tr = driver.find_element_by_css_selector('tr.Selectable')
                tr.click()
                time.sleep (sleep_sec)

处理Alter

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

def AcceptAlert(driver):
    try:
        WebDriverWait(driver, 3).until(EC.alert_is_present(),
                                       'Timed out waiting for PA creation ' +
                                       'confirmation popup to appear.')

        alert = driver.switch_to_alert()
        alert.accept()
        print ("alert accepted")
    except TimeoutException:
        print ("no alert")

处理弹出的其他窗口

    main_window_handle = None
    while not main_window_handle:
        main_window_handle = driver.current_window_handle
    driver.find_element_by_name('CMD_TEMPLATES').click()
    time.sleep(sleep_sec)
    signin_window_handle = None
    while not signin_window_handle:
        for handle in driver.window_handles:
            if handle != main_window_handle:
                signin_window_handle = handle
                break
    driver.switch_to.window(signin_window_handle)
    driver.find_element_by_name('bcnClose').click()
    driver.switch_to.window(main_window_handle)

所寻找的网页元素不存在

            try:
                tr = driver.find_element_by_css_selector('tr.Selectable')
                tr.click()
                time.sleep (sleep_sec)

                tr=driver.find_element_by_xpath("//tbody[@id='ListBody']/tr[1]")
                td=tr.find_elements(By.TAG_NAME, "td")[2]

                if td.text =='':
                    print('There is no policy')
                    return    

                else:

                    tr.click()
                    time.sleep (sleep_sec_short)

                    driver.find_element_by_name('btnOK').click() 
                    print('Choose existing policy')

            except NoSuchElementException:
                print('there is no item')
                return   

以上是比较基础的东西,我用了一个礼拜写完完整的测试脚本。Regression 从原来的十天,下降到一天半到两天。

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值