pythonUI自动化-02

# coding=utf-8

#1.窗口的操作
import time
from  selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.maximize_window()
driver.find_element(By.ID,'kw').send_keys('python')
driver.find_element(By.ID,'su').click()
time.sleep(3)
driver.back()            #返回操作

driver.find_element(By.ID,'kw').send_keys('python')
time.sleep(3)
driver.refresh()                            #刷新新窗口

#新开一个窗口页面
win="window.open('http://cms.duoceshi.cn/cms/manage/login.do')"             #打开一个新开的窗口访问cms
driver.execute_script(win)

driver.get('https://www.jd.com/')
'''
get():在原始页面上访问一个新页面
window.open():原始的基础上打开的一个新窗口访问地址
'''
win1="window.open('https://www.jd.com/')"
driver.execute_script(win1)

win2="window.open('https://www.taobao.com/')"
driver.execute_script(win2)
#窗口的切换,通过获取所有窗口的handle,使用索引进行切换
driver.switch_to.window(driver.window_handles[-1])
print(driver.title)

time.sleep(3)
# driver.close()             #关闭当前页面
driver.quit()               #关闭浏览器

#2.下拉定位select标签
from   selenium.webdriver.support.select import Select    #导入select类,用户定位select下拉框
import time
from  selenium import webdriver
from selenium.webdriver.common.by import By
# driver=webdriver.Chrome()
# driver.get('http://127.0.0.1:8020/duoceshi31/js/%E7%BB%83%E4%B9%A0.html')              #打开携程网
# driver.maximize_window()

#直接选择对应内容
# driver.find_element(By.XPATH,'/html/body/form/table/tbody/tr[7]/td[2]/select/option[7]').click()


# ele=driver.find_element(By.XPATH,'/html/body/form/table/tbody/tr[7]/td[2]/select')          #定位到下拉框元素

#通过select_by_value属性选择下拉框内容
# Select(ele).select_by_value('5')                              #选择第五个values的值的大学

#通过可见文本select_by_visible_text选择下拉框内容
# Select(ele).select_by_visible_text('清华大学')

#通过索引进行定位,选择下拉框内容
# Select(ele).select_by_index(3)


#3.弹框操作:告警或者确认型弹框
# driver=webdriver.Chrome()
# driver.get('http://www.baidu.com/')
# driver.maximize_window()
# driver.find_element(By.ID,'s-usersetting-top').click()
# time.sleep(2)
# driver.find_element(By.LINK_TEXT,'搜索设置').click()
# # time.sleep(2)
# driver.find_element(By.XPATH,'/html/body/div[1]/div[6]/div/div/div/div[1]/form/div/ul/li[6]/div/a[2]').click()
# btn=driver.switch_to.alert                      #切换到弹框
# btn.accept()                                   #点击确定按钮
# time.sleep(2)
'''
1.通过 btn=driver.switch_to.alert 切换弹框,并且定义个对象
2.对弹框内容进行操作:
btn.accept()   点击确定操作
btn.dismiss()  点击取消操作
btn.send_keys()  传文件或者输入内容
'''
# btn.dismiss()                                 #点击取消按钮
# btn.send_keys()                               #传文件或者输入内容
# time.sleep(2)
# driver.close()


#4.selenium中等待
'''
1.强制等待:time模块中的sleep,sleep(3) 强制等待3秒执行下一条代码,不管页面元素是否加载出来
2.隐试等待:智能等待,作用于全局,implicitly_wait(5),最长等待时间为5秒,在5秒内任意时间加载完成元素都会继续执行代码,超出等待时间跑出异常
3.显示等待:作用于指定元素,设置超时时间和隔间检测时间,判断元素是否加载,超出等待时间就抛出异常
'''
# from   selenium.webdriver.support.select import Select    #导入select类,用户定位select下拉框
# import time
# from  selenium import webdriver
# from selenium.webdriver.common.by import By
# driver=webdriver.Chrome()
# driver.get('http://www.baidu.com/')              #打开携程网
# driver.maximize_window()

#隐试等待,最长时间为10秒,在等待时间内元素加载完毕则继续执行
# driver.implicitly_wait(10)

#强制等待10秒,不管元素加载情况,10秒之后继续执行
# time.sleep(10)

#显示等待
'''
until():等待元素加载
until_not():等待元素不加载
WebDriverWait(driver,10,1):10:超时时间,1:间隔检测时间,超出时间还未加载出元素就会抛出一个timeout异常
'''

# from selenium.webdriver.support.wait import WebDriverWait   #导入WebDriverWait,用于设置等待页面元素超时时间和轮询时间
# from selenium.webdriver.support import expected_conditions as EC  #取别名为EC
# WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.ID,'kw')))
#
# driver.find_element(By.ID,'kw').send_keys('python')

#5.iframe框定位
'''
如果定位元素在iframe框中,需要先试用switch_to.frame()切换到iframe框,然后再对元素进行定位
如果iframe框还存在iframe框,需要使用switch_to.frame()一层一层切换
进入到iframe框之后需要定位框外的内容需要退出iframe框,使用switch_to.default_content()退出iframe
不管进行的是几层iframe,退出的时候直接退出到最外面

'''
# from   selenium.webdriver.support.select import Select    #导入select类,用户定位select下拉框
# import time
# from  selenium import webdriver
# from selenium.webdriver.common.by import By
# driver=webdriver.Chrome()
# driver.get('http://mail.163.com/')              #打开163网址
# driver.maximize_window()
# driver.implicitly_wait(5)

#先定位iframe框  第一种:正常元素定位
# iframe=driver.find_element(By.XPATH,'/html/body/div[3]/div[3]/div[1]/div/div[3]/div[1]/div[1]/iframe')     #定位iframe框
# driver.switch_to.frame(iframe)                #切换到iframe框

#通过索引的方式定位iframe框:查看需要切换的iframe标签在页面中是第几个出现,通过索引表示
# driver.switch_to.frame(0)
#
# driver.find_element(By.NAME,'email').send_keys('15927773643')
# driver.find_element(By.ID,'pwdtext').send_keys('lwf19961214')
#
# driver.switch_to.default_content()                   #退出iframe框

#6.滚动条定位
# from   selenium.webdriver.support.select import Select    #导入select类,用户定位select下拉框
# import time
# from  selenium import webdriver
# from selenium.webdriver.common.by import By
# driver=webdriver.Chrome()
# driver.get('https://www.jd.com/')              #打开163网址
# driver.maximize_window()
# driver.implicitly_wait(5)
# js="window.scrollTo(0,10000)"  #两个参数,第一个参数表示滚动向右移动的位置,第二个参数向下移动的位置
# js="window.scrollTo(0,800)"
# driver.execute_script(js)
# time.sleep(5)
# driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div/div[2]/div/div[9]/div/a[1]/div/img').click()
# time.sleep(3)
# driver.quit()

#7.keys类,可以模拟键盘的一些按键操作
# from   selenium.webdriver.support.select import Select    #导入select类,用户定位select下拉框
# import time
# from  selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.keys import Keys   #导入Key类
# driver=webdriver.Chrome()
# driver.get('https://www.baidu.com/')              #打开163网址
# driver.maximize_window()
# driver.implicitly_wait(5)
# driver.find_element(By.ID,'kw').send_keys('python')
# time.sleep(2)
# driver.find_element(By.ID,'kw').send_keys(Keys.BACK_SPACE)    #类似于键盘上的BACKSPACE按键删除一个字符
# driver.find_element(By.ID,'kw').send_keys(Keys.CONTROL,'a')     #a :全选  v:粘贴   c:复制   x:剪切
# time.sleep(2)
# driver.find_element(By.ID,'kw').send_keys(Keys.CONTROL,'x')
# time.sleep(2)
# driver.find_element(By.ID,'kw').send_keys(Keys.CONTROL,'v')

# time.sleep(2)
# driver.quit()



import time
from  selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class   Cms():
    def  __init__(self):
        self.driver=webdriver.Chrome()
        self.driver.get('http://cms.duoceshi.cn/cms/manage/login.do')
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
    def login(self):
        self.driver.find_element(By.ID,'userAccount').send_keys('admin')
        self.driver.find_element(By.ID,'loginPwd').send_keys('123456')
        self.driver.find_element(By.ID,'loginBtn').click()
    def yonghu(self):
        self.driver.find_element(By.CLASS_NAME,'icon-user').click()     #点击用户中心
        time.sleep(1)
        self.driver.find_element(By.XPATH,'/html/body/div/aside/div/dl[1]/dd/ul/li[1]/a').click()   #点击用户管理
        time.sleep(2)
        iframe = self.driver.find_element(By.XPATH,'/html/body/div/section/div[2]/div[2]/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH,'/html/body/div/div[2]/span[1]/a[2]').click()     #点击添加用户
        time.sleep(2)

        iframe=self.driver.find_element(By.XPATH,'/html/body/div[3]/div/iframe')    #进入第2层iframe
        self.driver.switch_to.frame(iframe)
        time.sleep(2)
        self.driver.find_element(By.XPATH,'/html/body/div/div/form/table/tbody/tr[1]/td/input[2]').send_keys('卢本伟')
        self.driver.find_element(By.XPATH,'/html/body/div/div/form/table/tbody/tr[2]/td/label[2]/input').click()
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[3]/td/input').send_keys('13801010101')
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[4]/td/input').send_keys('666666@qq.com')
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[5]/td/input').send_keys('lbw2')
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[6]/td/input').send_keys('123456')
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[7]/td/input').send_keys('123456')
        time.sleep(1)
        self.driver.find_element(By.XPATH,'/html/body/div/div/form/table/tbody/tr[8]/td/button').click()

    def del1(self):
        self.driver.find_element(By.CLASS_NAME, 'icon-user').click()  # 点击用户中心
        time.sleep(1)
        self.driver.find_element(By.XPATH, '/html/body/div/aside/div/dl[1]/dd/ul/li[1]/a').click()  # 点击用户管理
        time.sleep(2)
        iframe = self.driver.find_element(By.XPATH, '/html/body/div/section/div[2]/div[2]/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '/html/body/div/div[1]/input[3]').send_keys('卢本伟')  # 点击搜索框
        self.driver.find_element(By.XPATH, '/html/body/div/div[1]/button').click()  # 点击搜索
        self.driver.find_element(By.XPATH,'/html/body/div/table/tbody/tr/td[9]/a[4]/i').click()  #点击删除用户
        time.sleep(1)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[1]/span[2]/a[2]').click()  #点击取消
        #self.driver.find_element(By.XPATH, '/html/body/div[3]/div[1]/span[2]/a[1]').click() #点击确认

    def pwd(self):
        self.driver.find_element(By.CLASS_NAME, 'icon-user').click()  # 点击用户中心
        time.sleep(1)
        self.driver.find_element(By.XPATH, '/html/body/div/aside/div/dl[1]/dd/ul/li[1]/a').click()  # 点击用户管理
        time.sleep(2)
        iframe = self.driver.find_element(By.XPATH, '/html/body/div/section/div[2]/div[2]/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '/html/body/div/table/tbody/tr[1]/td[9]/a[3]/i').click() #点击修改密码,ID为1的密码
        iframe = self.driver.find_element(By.XPATH, '/html/body/div[3]/div/iframe')  # 进入第2层iframe
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[1]/td/input[2]').send_keys('654321') #输入密码
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[2]/td/input').send_keys('654321')    #确认密码
        self.driver.find_element(By.XPATH,'/html/body/div/form/table/tbody/tr[3]/td/button').click()    #点击确认按钮



    def sousuo(self):
        self.driver.find_element(By.CLASS_NAME, 'icon-user').click()  # 点击用户中心
        time.sleep(1)
        self.driver.find_element(By.XPATH, '/html/body/div/aside/div/dl[1]/dd/ul/li[1]/a').click()  # 点击用户管理
        time.sleep(2)
        iframe = self.driver.find_element(By.XPATH, '/html/body/div/section/div[2]/div[2]/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH,'/html/body/div/div[1]/input[3]').send_keys('卢本伟')    #点击搜索框
        self.driver.find_element(By.XPATH,'/html/body/div/div[1]/button').click()       #点击搜索
        self.driver.find_element(By.XPATH, '/html/body/div/table/tbody/tr/td[9]/a[2]/i').click()  # 点击编辑,
        iframe = self.driver.find_element(By.XPATH, '/html/body/div[3]/div/iframe')  # 进入第2层iframe
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[1]/td/input[2]').click()  # 点击名字
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[1]/td/input[2]').send_keys(Keys.CONTROL, 'a')  # 全选
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[1]/td/input[2]').send_keys(Keys.BACK_SPACE)  # 删除
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[1]/td/input[2]').send_keys('卢本伟哪个班的')  # 改名
        self.driver.find_element(By.XPATH, '/html/body/div/div/form/table/tbody/tr[8]/td/button').click()  # 点击确认

    def xtgl(self):
        self.driver.find_element(By.CLASS_NAME,'icon-cogs').click()        #点击系统管理
        self.driver.find_element(By.XPATH, '//*[@id="menu-system"]/dd/ul/li[1]/a').click()  # 点击文章管理
        iframe = self.driver.find_element(By.XPATH, '//*[@id="iframe_box"]/div[2]/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        time.sleep(2)
        self.driver.find_element(By.XPATH,'/html/body/div/div[1]/span[1]/a').click()     #点击添加栏目
        iframe = self.driver.find_element(By.XPATH, '/html/body/div[3]/div/iframe')  # 定位iframe框
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[1]/td[2]/input[2]').send_keys('这个是栏目名称')
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[2]/td[2]/input').send_keys('这个是栏目目录')
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[3]/td[2]/textarea').send_keys('这个是栏目描述')
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[4]/td[2]/textarea').send_keys('这个标题')
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[5]/td[2]/textarea').send_keys('这个关键词')
        self.driver.find_element(By.XPATH, '/html/body/div/form/table/tbody/tr[6]/td[2]/textarea').send_keys('这个关键词描述')
        self.driver.find_element(By.XPATH,'/html/body/div/form/table/tbody/tr[7]/td/div/button').click()    #点击保存




if __name__ == '__main__':
    a=Cms()
    a.login()
    # a.xtgl()
    a.sousuo()

 

posted @ 2023-04-19 22:54  jormen  阅读(27)  评论(0编辑  收藏  举报