南阳网站开发,永川网站建设熊掌号,自己做网站能宣传自己的产品吗,开发app和微网站有哪些介绍 测试的系统#xff1a;白月黑羽网站的测试系统(白月SMS系统) 测试内容#xff1a;点击【学习教程】链接跳转到白月黑羽网站#xff0c;获取此网站上的标题#xff0c;然后回到原来的系统。 所涉及的知识点#xff1a;frame切换/窗口切换 这个iframe元素非常的特殊&…介绍测试的系统白月黑羽网站的测试系统(白月SMS系统)测试内容点击【学习教程】链接跳转到白月黑羽网站获取此网站上的标题然后回到原来的系统。所涉及的知识点frame切换/窗口切换这个iframe元素非常的特殊在html语法中frame元素或者iframe元素的内容会包含一个被嵌入的零一份html文档。在我们使用selenium打开一个网页是我们的操作范围缺省是当前的html,并不包含被嵌入的html文档里面的内容。如果我们要操作被嵌入的html文档中的元素就必须切换操作范围到被嵌入的文档中。切换到Frame(3种方式)# 方式1通过索引切换第1个iframe从0开始driver.switch_to.frame(0)# 方式2通过 name 或 id 属性切换driver.switch_to.frame(frame-name)# nameframe-namedriver.switch_to.frame(frame-id)# idframe-id# 方式3通过 WebElement 对象切换最灵活推荐iframedriver.find_element(By.CSS_SELECTOR,iframe.class-name)driver.switch_to.frame(iframe)切回主文档/父级Frame# 切回父级 Frame如果有多层嵌套只向上退一层driver.switch_to.parent_frame()# 切回最外层的主文档彻底退出所有 iframedriver.switch_to.default_content()自动化测试程序fromseleniumimportwebdriverfromtimeimportsleepfromselenium.webdriver.common.byimportByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECclassRedirect:def__init__(self):self.driverwebdriver.Chrome()defLogin(self,username,password):self.driver.get(http://127.0.0.1/mgr/sign.html)usernameElementself.driver.find_element(By.ID,username)usernameElement.send_keys(username)sleep(2)passwordElementself.driver.find_element(By.ID,password)passwordElement.send_keys(password)sleep(2)# 点击登录submitElementself.driver.find_element(By.XPATH,//div[classcol-xs-12]/button)submitElement.click()sleep(2)print(登录成功~)defRedirectByhy(self):#mainWindow变量保存当前窗口的句柄mainWindowself.driver.current_window_handle#定位跳转的新窗口链接iframeself.driver.find_element(By.XPATH,//footer/div/a)hrefiframe.get_attribute(href)iframe.click()# 等待新窗口出现窗口数量增加WebDriverWait(self.driver,10).until(EC.number_of_windows_to_be(2))#切换到新窗口forhandleinself.driver.window_handles:ifhandle!mainWindow:self.driver.switch_to.window(handle)breakself.driver.get(f{href})sleep(2)titleElementsself.driver.find_elements(By.XPATH,//div[idnav-topics]/a)fortitleintitleElements:print(title.text)print(【本次测试结束】)if__name____main__:ReRedirect()Re.Login(byhy,88888888)Re.RedirectByhy()