python(三):python自动化测试定位

webdriver 提供了八种元素定位方法:
id、name、class name、tag name、link text、partial link text、xpath、css selector
在 Python 语言中对应的定位方法如下:
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
使用:

import sys
import urllib
import urllib2
from selenium import webdriver
import os
import webbrowser
import time,unittest
print time.ctime()#打印当前时间


#登录
def login(un,url):
driver = webdriver.Ie()
driver.implicitly_wait(10)
driver.get(url)
driver.find_element_by_name("username").clear()
driver.find_element_by_name("username").send_keys(un)
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_name("subEnter").click()
time.sleep(3)
# 获取断言信息进行断言
username=driver.find_element_by_xpath("//html/body/table/tbody/tr/td/*/tbody/tr/td/table/tbody/tr/td/b/font").text
print type(username)
username1=username
print type(username1)
#print username
print "%s :welcome to the system"%username1
f=open(r'D:\\python\\a.txt','ab')
f.write(username1.encode('gb2312'))
f.write(' :welcome to the system\n'+'\n')
f.close()
driver.get_screenshot_as_file("D:\\error.jpg")#截图功能
if un.encode('gb2312')==username.encode('gb2312'):
print "success"
else:
print "fail"
#退出网站
def exit(un):
driver.find_element_by_class_name("personSet").click()
f=open(r'D:\\python\\a.txt','ab')
f.write(un.encode('gb2312'))
f.write(' :exit the system\n'+'time.ctime()'+'\n')
print time.ctime()#打印当前时间
f.close()

#从txt获取用户名
source=open("D:\\python\\name.txt","r")#用户名文件
values=source.readline()#读取用户名 ,全部用户名readlines
source.close()
un=values
url="http://192.168.1.80:7001/"
#un="chenyang"
login(values,url)
'''
多个用户名循环登录
for un in values:
try:
login(un,url)
except IOError:
print "登录失败!" #异常打印
'''

posted @ 2018-06-30 12:42  拉努斯石  阅读(235)  评论(0编辑  收藏  举报