zoukankan      html  css  js  c++  java
  • webdriver高级应用- 在ajax方式产生的浮动框中,单击选择包含某个关键字的选项

    Ajax简介:

    Ajax:局部刷新,原理上也是一个js,js调用服务器的远程接口刷新局部页面数据。

    Ajax = 异步 JavaScript 和 XML标准通用标记语言的子集)。

    Ajax 是一种用于创建快速动态网页的技术。

    Ajax 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。 [1] 

    通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。

    传统的网页(不使用 Ajax)如果需要更新内容,必须重载整个网页页面。

     

    方法一:因为浮动窗的内容可能会时常发生变化,如果想固定选择浮动框中的某一项,比如第三项,代码如下:

    #encoding=utf-8
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    import traceback
    import unittest
    import time
    
    class TestDemo(unittest.TestCase):
    
        def setUp(self):
            # 启动Chrome浏览器
            self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
    
        def test_AjaxDivOptionByIndex(self):
            url = "http://www.sogou.com/"
            # 访问sogou的首页
            self.driver.get(url)
            try:
                # 找到搜狗首页中的搜索输入框页面元素
                searchBox = self.driver.find_element_by_id("query")
                # 在搜索输入框中输入“光荣之路”
                searchBox.send_keys(u"光荣之路")
                # 等待2秒,以便悬浮框加载完成
                time.sleep(2)
                # 查找浮动框中的第三选项,只要更改li[3]中的索引数字,
                # 就可以实现任意单击选项浮动框中的选项。注意,索引从1开始
                suggetion_option = self.driver. 
                    find_element_by_xpath("//*[@id='vl']/div[1]/ul/li[3]")
                #print suggetion_option.text
                # 点击找到的选项
                suggetion_option.click()
                time.sleep(3)
            except NoSuchElementException, e:
                # 打印异常堆栈信息
                print traceback.print_exc()
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()

    方法二:通过匹配模糊内容选择悬浮框中的选项,代码如下:

    #encoding=utf-8
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    import traceback
    import unittest
    import time
    
    class TestDemo(unittest.TestCase):
    
        def setUp(self):
            # 启动Chrome浏览器
            self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
    
        def test_AjaxDivOptionByWords(self):
            url = "http://www.sogou.com/"
            # 访问sogou的首页
            self.driver.get(url)
            try:
                # 找到搜狗首页中的搜索输入框页面元素
                searchBox = self.driver.find_element_by_id("query")
                # 在搜索输入框中输入“光荣之路”
                searchBox.send_keys(u"光荣之路")
                # 等待2秒,以便悬浮框加载完成
                time.sleep(2)
                # 查找内容包含“篮球电影”的悬浮选项
                suggetion_option = self.driver.
                    find_element_by_xpath("//ul/li[contains(., '电影')]")
                # 点击找到的选项
                suggetion_option.click()
                time.sleep(3)
            except NoSuchElementException, e:
                # 打印异常堆栈信息
                print traceback.print_exc()
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()

    方法三:通过模拟键盘下箭头进行选择悬浮窗选项,代码如下:

    #encoding=utf-8
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import unittest
    import time
    
    class TestDemo(unittest.TestCase):
    
        def setUp(self):
            # 启动Chrome浏览器
            self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
    
        def test_AjaxDivOptionByKeys(self):
            url = "http://www.sogou.com/"
            # 访问sogou的首页
            self.driver.get(url)
            # 找到搜狗首页中的搜索输入框页面元素
            searchBox = self.driver.find_element_by_id("query")
            # 在搜索输入框中输入“光荣之路”
            searchBox.send_keys(u"光荣之路")
            # 等待2秒,以便悬浮框加载完成
            time.sleep(2)
            for i in range(3):
                # 选择悬浮框中中第几个联想关键词选项就循环几次
                # 模拟键盘点击下箭头
                searchBox.send_keys(Keys.DOWN)
                time.sleep(0.5)
            # 当按下箭头到想要选择的选项后,再模拟键盘点击回车键,选中该选项
            searchBox.send_keys(Keys.ENTER)
            time.sleep(3)
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    【Alpha阶段】第四次Scrum Meeting
    【Alpha阶段】第三次Scrum Meeting
    【Alpha阶段】第二次Scrum Meeting
    项目复审——Alpha阶段
    团队作业7-Alpha冲刺之事后诸葛亮
    团队作业6-展示博客
    Alpha阶段发布说明
    团队作业5-测试与发布
    冲刺博客-8
    冲刺博客-7
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8716045.html
Copyright © 2011-2022 走看看