zoukankan      html  css  js  c++  java
  • 检查页面元素是否存在

     1 # -*- coding:utf-8 -*-
     2 
     3 """
     4 检查元素是否存在
     5 isElementPresent():检查页面的元素上是否存在
     6 """
     7 
     8 from selenium import webdriver
     9 from selenium.common.exceptions import NoSuchElementException
    10 from selenium.webdriver.common.by import By
    11 
    12 class IsElement(object):
    13     def __init__(self):
    14         self.driver = webdriver.Firefox()
    15         
    16     def isElementPresent(self, how, what):
    17         try:
    18             self.driver.find_element(how, what)
    19         except NoSuchElementException as e:
    20             return False        
    21         return True
    22     
    23     def testisElementPresent(self):
    24         self.driver.get('http://www.baidu.com')
    25         self.driver.find_element_by_id('kw').send_keys('selenium')
    26         
    27         if self.isElementPresent(By.ID, 'su'):
    28             self.driver.find_element(By.ID, 'su').click()
    29         else:
    30             raise '元素不存在'
    31         
    32 if __name__ == '__main__':
    33     test = IsElement()
    34     test.testisElementPresent()
    35             
    36         
  • 相关阅读:
    帆软报表实现全选全不选的功能
    knowledge_others
    skills_kafka
    skills_operation
    problems_others
    skills_windows
    c语言标识符
    快速排序法
    字符串处理scanf("%d%*c",&n);
    Byte.parseByte(String s,int radix)的解释
  • 原文地址:https://www.cnblogs.com/Roger1227/p/3175529.html
Copyright © 2011-2022 走看看