zoukankan      html  css  js  c++  java
  • Selenium WebDriver-判断页面中某一元素是否已经显示,通常用于断言

    判断界面中某一元素是否已经呈现,多用于断言,代码如下:

    #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 isElementPresent(self, by, value):
            # 从selenium.common.exceptions模块导入NoSuchElementException异常类
            from selenium.common.exceptions import NoSuchElementException
            try:
                element = self.driver.find_element(by=by, value=value)
            except NoSuchElementException, e:
                # 打印异常信息
                print e
                # 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False
                return False
            else:
                # 没有发生异常,表示在页面中找到了该元素,返回True
                return True
        
        
        def test_isElementPresent(self):
            url = "http://www.sogou.com"
            # 访问sogou首页
            self.driver.get(url)
            # 判断页面元素id属性值为“query”的页面元素是否存在
            res = self.isElementPresent("id", "query")
            if res is True:
                print u"所查找的元素存在于页面上!"
            else:
                print u"页面中未找到所需要的页面元素!"
    
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    Java并发编程实战3-可见性与volatile关键字
    Java并发编程实战2-线程安全
    Java词频统计
    Java中split的对象被特殊字符(.或|)分隔
    Kernel Space与User Space(转)
    [Python]Pandas简单入门(转)
    Python的自省机制
    谈谈Python中元类Metaclass(二):ORM实践
    Python的垃圾回收机制
    [Python]Pip换源以及设置代理
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8709152.html
Copyright © 2011-2022 走看看