Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换

Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换#encoding=utf-8importunittestimporttimefromseleniumimportwebdriverfromselenium.webdriverimportActionChainsclassVisitSogouByIE(unittest.T

大家好,欢迎来到IT知识分享网。Selenium

#encoding=utf-8
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains

class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #启动IE浏览器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
        
    def test_identifyPopUpWindowByPageSource(self):
        # 导入多个异常类型
        from selenium.common.exceptions import NoSuchWindowException,TimeoutException
        # 导入期望场景类
        from selenium.webdriver.support import expected_conditions as EC
        # 导入By类
        from selenium.webdriver.common.by import By
        # 导入WebDriverWait类
        from selenium.webdriver.support.ui import WebDriverWait
        # 导入堆栈类
        import traceback
        # 导入时间模块
        import time
        url = "http://127.0.0.1/test_popup_window.html"
        # 访问自动以测试网页
        self.driver.get(url)
        # 显示等待找到页面上链接文字为“sogou 搜索”的链接元素,找到后点击它
        WebDriverWait(self.driver, 10, 0.2).until(EC.element_to_be_clickable \
                                                      ((By.LINK_TEXT, 'sogou 搜
'))).click()
        # 获取当前所有打开的浏览器窗口句柄
        all_handles = self.driver.window_handles
        # 打印当前浏览器窗口句柄
        print self.driver.current_window_handle
        # 打印打开的浏览器窗口的个数
        print len(all_handles)
        # 等待2秒,以便更好查看效果
        time.sleep(2)
        # 如果存储浏览器窗口句柄的容器不对空,再遍历all_handles中所有的浏览器句柄
        if len(all_handles) > 0:
            try:
                for windowHandle in all_handles:
                    # 切换窗口
                    self.driver.switch_to.window(windowHandle)
                    # 获取当前浏览器窗口的页面源代码
                    pageSource = self.driver.page_source
                    if u"搜狗搜索" in pageSource:
                        # 显示等待页面搜索输入框加载完成,
                        # 然后输入“sogou 首页的浏览器窗口被找到”
                        WebDriverWait(self.driver, 10, 0.2).until \
                            (lambda x: x.find_element_by_id("query")). \
                            send_keys(u"sogou 首页的浏览器窗口被找到")
                        time.sleep(2)
            except NoSuchWindowException, e:
                # 如果没找到浏览器的句柄,会抛出NoSuchWindowException异常,
                # 打印异常的堆栈信息
                print traceback.print_exc()
            except TimeoutException, e:
                # 显示等待超过规定时间后抛出TimeoutException异常
                # 打印异常的堆栈信息
                print traceback.print_exc()
        # 将浏览器窗口切换回默认窗口
        self.driver.switch_to.window(all_handles[0])
        # 断言当前浏览器窗口页面源代码中是否包含“你爱吃的水果么?”关键内容
        self.assertTrue(u"你爱吃的水果么?" in self.driver.page_source)
    



    def tearDown(self):
        # 退出IE浏览器
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/27562.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信