Python-po模式-:每一个页面用例必须能够独立运行(web自动化)_xing2516_新浪博客

#此模块为po.py
import requests
import time
from selenium import webdriver
'''
po设计模式-Python
page object:页面对象化
为什么用?:封装调用
1.每一个页面用例必须能够独立运行
'''
class web():
def __init__(self):#使用对象的封装,要用self
self.driver=webdriver.Chrome(executable_path='./chromedriver.exe')
self.driver.implicitly_wait(10)#隐式等待,是设置的全局等待(没找到某个元素,会过一秒找一次,直到设置的时间10秒)
#打开网址

def login(self,u,p):
self.driver.get("https://u.ppkao.com/user/")
# 找到登录框
el1 = self.driver.find_element_by_xpath('//*[@id="main"]/div/div/div/div[2]/div/form/div[1]/div/div[1]/input')
# 输入用户名
el1.send_keys(u)
# 输入密码
el2 =self. driver.find_element_by_xpath('//*[@id="main"]/div/div/div/div[2]/div/form/div[2]/div/div[1]/input') \
.send_keys(p)
# 点击登录按钮
el3 = self.driver.find_element_by_xpath('//*[@id="main"]/div/div/div/div[2]/div/form/div[3]/div/bu
tton').click()
        # 登录成功后,找到首页或用户名,print成功
time.sleep(1) # sleep(): 强制等待(页面加载慢,找不到元素);一般是隐式等待不起作用,用他
try: # 抛异常,不让他报错
el4 =self. driver.find_element_by_xpath( '//*[@id="main"]/div/div[2]/div[1]/div[3]/div[3]/div/div/div/a/span').text
except:
el4 = None
if el4 == '9992516':
print('登录成功')
else:
print('登录失败')
def userinfo(self):
# 点击个人中心
self.login('9992516', '8882516')#调用上一个登录方法函数,为保证单独运行userinf()
self.driver.get('https://u.ppkao.com/user/#/ownspace')#确保先到个人中心
el5 = self.driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[3]/div[3]/div/div/div/a/span') \
.click()
self.driver.find_element_by_xpath('/html/body/div[11]/ul/li[1]').click()
self.driver.find_element_by_xpath('//*[@id="fileinput1"]').send_keys('C:\\Users\\by\Desktop\\49.png')
time.sleep(2)

# 点击上传确定按钮
self.driver.find_element_by_xpath('/html/body/div[15]/div[2]/div/div/div[3]/button[2]/span')
.click()
def useredit(self):
self.login('99925161','88825162')
self.driver.get('https://u.ppkao.com/user/#/ownspace')
#用户姓名
self.driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/div/div/div/div[2]/div/form/div[2]/div/div/div/input')
.send_keys('timxing')
#选择性别
self.driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/div/div/div/div[2]/div/form/div[4]/div/div/label[2]/span[1]/input')
.click()

#点击保存按钮
self.driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/div/div/div/div[2]/div/form/div[8]/button[2]')
.click()
#self.driver.find_elements_by_css_selector()
#time.sleep(5)
#self.driver.quit()#停留5秒自动关闭浏览器


 
 
#此模块为runner.py
from webzi import po
po=po.web()
#po.login('9992516','88825162')
#po.userinfo()
po.useredit()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值