zoukankan      html  css  js  c++  java
  • webdriver中判断元素是否存在的方法

    selenium.webdriver中没有内置的判断元素是否存在的方法,所以定义一个方法,如果找到该元素则返回True,否则返回False:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import NoSuchElementException
    
    class Demo:
         def __init__(self):     # 初始化
            self.driver = webdriver.Chrome()    #实例化
            self.driver.get('http://www.demo.com')  #打开指定网址
    
        def is_element_present(self, how, what):    
            try:
                self.driver.find_element(by=how, value=what)
            except NoSuchElementException:
                return False
            return True
    
    <html>
        <head>
        </head>
        <body>
            <a 
            href='http://www.baidu.com' 
            id='id_01' 
            class='class_01' 
            name='name_01'
            >这是一个链接</a>
        </body>
    </html>
    
    #通过id
    is_element_present(By.ID,'id_01')
    #通过name
    is_element_present(By.NAME,'name_01')
    # 通过class_name
    is_element_present(By.CLASS_NAME,'class_01')
    # 通过tag_name
    is_element_present(By.TAG_NAME,'a')
    # 通过
    
  • 相关阅读:
    开淘店记录
    广告轮播效果
    loading事件加载效果
    正则表达式摘要
    cookie存取数据分析
    js 空格与回车处理
    数据对象型转换为数组型
    变换闪烁效果
    eclipse配置新环境
    五小时轻松入门Python
  • 原文地址:https://www.cnblogs.com/milesma/p/12021673.html
Copyright © 2011-2022 走看看