Python Selenium之UI自动化测试

谷歌浏览器驱动下载地址:http://npm.taobao.org/mirrors/chromedriver/

from time import sleep
from selenium import webdriver
class work:
def __init__(self):#每次进行网页操作时都要进行登录,所以把登录初始化
self.driver = webdriver.Chrome()#使用谷歌浏览器
self.driver.get('http://localhost:8080/entropy/xapp/login/login.xsp')#获取网址
self.driver.maximize_window()#将浏览器页面放大
self.driver.find_element_by_id("userName").send_keys('admin')#通过定位输入框id输入用户名,element就是页面元素的对象
self.driver.find_element_by_name("userPass").send_keys(1)#通过定位用户名输入框name输入密码
self.driver.find_element_by_id("loginSubmit").click()#点击确认按钮
sleep(1)#停顿1秒给程序一秒缓冲的机会,以免定位不到下面的操作,减少错误
def server(self):
sleep(1)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/ul/li[16]/div/div').click()#基础数据
sleep(1)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/ul/li[16]/ul/li[7]').click()#SIP服务器
sleep(1)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/div[1]/button[1]').click()#新建
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div/div/div[2]/div[1]/div/input').send_keys('9000服务器')#名称
sleep(1)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/div/form/div[2]/div[1]/div/div[2]/div/div/input').send_keys('192.168.3.100')#SIP服务器地址
sleep(1)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/div/form/div[2]/div[2]/div/div[2]/div/div/span[3]/span').click()#是否主服务器
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/div/div/button[1]').click()#确定
try:
sleep(7)
result = self.driver.switch_to.alert#对网页弹窗进行操作
print(result)
print(result.text())#打印出弹窗的文本
except Exception as e:
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/div/div[2]/div/h2/div/button[2]').click()#删除
sleep(3)
self.driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/button[2]').click()#确认
self.driver.quit()#关闭网页
raise e
(1)定位方式
find_element_by_id()
find_element_by_Xpath()
find_element_by_class_name()
find_element_by_name()
find_element_by_css_selector()#css选择器
find_element_by_link_text()#完整超链接
find_element_by_link_partial_text()#部分链接
find_element_by_tag_name ()#标签名
(2)操作
1.模拟浏览器、键盘

 2.模拟鼠标

(3)下拉框操作
select = Select(driver.find_element_by_name('status')) # 找到 name 的选项卡
select.select_by_index(1) # index 索引从 0 开始
select.select_by_value("0") # value是option标签的一个属性值,并不是显示在下拉框中的值
select.select_by_visible_text(u"xxx") # visible_text是在option标签文本的值,是显示在下拉框的值
(4)弹窗操作
alert = driver.switch_to_alert()
alert.accept() # 点击确认按钮
alert.dismiss() # 点击取消按钮
alert.text() # 返回alert/confirm/prompt中的文字信息
alert.send_keys(“hello”) # 向prompt中输入文字
(5)暂停浏览器页面活动
当我们的网页提示框几秒之后就消失了怎么办?

 第一步,打开F12抓包

第二步,选择sources(源)

第三步,点击右下方红色方框部分,暂停页面

(6)模拟键盘按键的按键名

NULL = 'ue000'                                 # 空键
CANCEL = 'ue001'                            # Cancel键,相当于ESC键
HELP = 'ue002'                                  # Help键
BACKSPACE = 'ue003'                     # BackSpace退格键
BACK_SPACE = BACKSPACE          # BackSpace退格键
TAB = 'ue004'                                    # Tab键
CLEAR = 'ue005'                               # 关闭NumLock的5键
RETURN = 'ue006'                            # Return回车键(Mac)
ENTER = 'ue007'                              # Enter回车键
SHIFT = 'ue008'           # Shift键
LEFT_SHIFT = SHIFT          # Shift键
CONTROL = 'ue009'           # Ctrl键
LEFT_CONTROL = CONTROL    # Ctrl键
ALT = 'ue00a'             # Alt键
LEFT_ALT = ALT          # Alt键
PAUSE = 'ue00b'           # Pause键
ESCAPE = 'ue00c'        # ESC键
SPACE = 'ue00d'        # 空格键
PAGE_UP = 'ue00e'         # PgUp键
PAGE_DOWN = 'ue00f'     # PgDn键
END = 'ue010'         # End键
HOME = 'ue011'          # Home键
LEFT = 'ue012'         # ← 键
ARROW_LEFT = LEFT     # ← 键
UP = 'ue013'          # ↑ 键
ARROW_UP = UP       # ↑ 键
RIGHT = 'ue014'         # → 键
ARROW_RIGHT = RIGHT     # → 键
DOWN = 'ue015'         # ↓ 键
ARROW_DOWN = DOWN    # ↓ 键
INSERT = 'ue016'           # Insert键
DELETE = 'ue017'          # Delete键
SEMICOLON = 'ue018'       # ; 键
EQUALS = 'ue019'         # = 键

driver.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[1]/div/div[2]/div/div/div[1]/div[1]/div[2]/input').send_keys('总单位')
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[1]/div/div[2]/div/div/div[1]/div[1]/div[2]/input').send_keys(Keys.RETURN)
原文地址:https://www.cnblogs.com/1527275083gj/p/14970040.html