网页资源 链接: https://pan.baidu.com/s/17XL2c2lkw_R6BD–VnOQqw?pwd=43dr 提取码:
43dr 复制这段内容后打开百度网盘手机App,操作更方便哦
框架之间切换
driver.switch_to.frame("idframe1") # 父切子 参数用id和name
# 子切子必须先转回父
driver.switch_to.parent_frame() # 子切父
driver.switch_to.frame("idframe2") # 父切子
练习
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("file:///D:/CarTest/web/Register.html")
driver.find_element(By.ID, 'user').send_keys('admin')
driver.switch_to.frame("idframe1") # 父切子 参数用idid=‘idframe1’
driver.find_element(By.ID, 'userA').send_keys('adminA')
# 子切子必须先转回父
driver.switch_to.parent_frame() # 子切父
driver.find_element(By.XPATH, '//*[@id="zc"]/a[1]').click()
driver.switch_to.frame("idframe2") # 父切子
driver.find_element(By.ID, 'userB').send_keys('userB')
sleep(4)
断言
断言 assert 预期==实际 没问题不会报错
a = driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[1]/td[3]').text
print(a)
assert a == '东方闪电的速度1'
等待
# 强制等待 写在操作里
sleep()
# 隐式等待
driver.implicitly_wait(10) # 写在开头 每个操作10秒
# 显式等待 写在开头
# 整个这句话的意思是,在5秒时间内,每隔0.5秒去定位一次元素(id="kw"的元素)。
# 如果5秒内能定位到,则继续执行;定位失败,则报错
WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.ID, "kw")))
=======================综合练习
# 登录名鹰OA系统 http://192.168.8.57:8080/QzhOA/login
# 完成 新增会议、断言、
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
# 框架的切换
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("file:///D:/CarTest/web/Register.html")
'''driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://192.168.8.57:8080/QzhOA/login")
# driver.find_element(By.NAME, 'username').send_keys('admin')
# driver.find_element(By.NAME, 'password').send_keys('123456')
driver.find_element(By.ID, 'btnSubmit').click() # 登录
sleep(1)
driver.find_element(By.XPATH, '//ul[@id="side-menu"]/li[7]/a/span[1]').click() # 会议管理
sleep(1)
driver.find_element(By.XPATH, '//*[@id="side-menu"]/li[7]/ul/li/a').click()
sleep(1)
driver.switch_to.frame('iframe21') # 切子
driver.find_element(By.XPATH, '//*[@id="toolbar"]/a[1]').click() # 新增会议
sleep(2)
driver.switch_to.frame('layui-layer-iframe1') # 切子
driver.find_element(By.XPATH, '//*[@id="meetingTheme"]').send_keys('环保3')
driver.find_element(By.XPATH, '//*[@id="meetingAddr"]').send_keys('上海')
driver.find_element(By.XPATH, '//*[@id="form-meeting-add"]/div[4]/div/div[2]/div[3]/div[3]/p').send_keys('哈哈哈哈哈哈')
driver.switch_to.parent_frame()
driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click() # 确定
sleep(2)
driver.switch_to.frame('iframe21') # 切子
driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[2]/div[4]/div[2]/ul/li[8]/a').click() # 140
sleep(1)
a = driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[4]/td[3]').text # ?
print(a)
assert a == '环保3'
sleep(3)
driver.quit()'''
# -------------------------------------------
# 修改会议、断言
'''driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://192.168.8.57:8080/QzhOA/login")
# driver.find_element(By.NAME, 'username').send_keys('admin')
# driver.find_element(By.NAME, 'password').send_keys('123456')
driver.find_element(By.ID, 'btnSubmit').click() # 登录
sleep(1)
driver.find_element(By.XPATH, '//ul[@id="side-menu"]/li[7]/a/span[1]').click() # 会议管理
sleep(1)
driver.find_element(By.XPATH, '//*[@id="side-menu"]/li[7]/ul/li/a').click()
sleep(1)
driver.switch_to.frame('iframe21') # 切子
sleep(1)
driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[1]/td[10]/a[1]').click() # 修改会议
sleep(2)
driver.switch_to.frame('layui-layer-iframe1') # 切子
driver.find_element(By.XPATH, '//*[@id="meetingTheme"]').clear()
driver.find_element(By.XPATH, '//*[@id="meetingTheme"]').send_keys('东方闪电的速度2')
sleep(1)
driver.switch_to.parent_frame()
# driver.switch_to.frame('layui-layer-iframe1') # 切子
driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click() # 确定
sleep(2)
# driver.switch_to.frame('iframe21') # 切子
sleep(1)
a = driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[1]/td[3]').text
print(a)
assert a == '东方闪电的速度1'
sleep(3)
driver.quit()'''
# --------------------------------
# 删除会议、断言
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://192.168.8.57:8080/QzhOA/login")
# driver.find_element(By.NAME, 'username').send_keys('admin')
# driver.find_element(By.NAME, 'password').send_keys('123456')
driver.find_element(By.ID, 'btnSubmit').click() # 登录
sleep(1)
driver.find_element(By.XPATH, '//ul[@id="side-menu"]/li[7]/a/span[1]').click() # 会议管理
sleep(1)
driver.find_element(By.XPATH, '//*[@id="side-menu"]/li[7]/ul/li/a').click()
sleep(1)
driver.switch_to.frame('iframe21') # 切子
sleep(1)
i = driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[2]/td[2]').text
driver.find_element(By.XPATH, '//*[@id="bootstrap-table"]/tbody/tr[1]/td[10]/a[2]').click() # 删除
sleep(2)
driver.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
sleep(1)
print(i)
assert i == 32
sleep(5)
driver.quit()