zoukankan      html  css  js  c++  java
  • 判断元素是否存在页面的两种不同写法

    直接上代码

    #coding:utf-8
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    
    def find_element(locator,timeout=10):
        '''定位方法封装成函数'''
        #lambda判断元素是否存在
        # element = WebDriverWait(driver, timeout,1).until(lambda x : x.find_element(*locator))
        
        #presence_of_element_located判断元素是否存在
        try:
            element = WebDriverWait(driver, timeout,1).until(EC.presence_of_element_located(locator))
        except TimeoutException:
            print("元素没有定位到%s" % str(locator))
        else:
            return element
    
    try:
        input_loc = ("id","kw1")
        button_loc = ("id","su")
        find_element(input_loc).send_keys("yoyo")
        find_element(button_loc).click()
        time.sleep(3)
    finally:
        driver.quit()
  • 相关阅读:
    深入理解hadoop之MapReduce
    centos关机与重启命令
    hadoop学习笔记(1)
    配置ssh免密码登录设置后还是提示需要输入密码
    js获得URL中的参数
    SQLite介绍
    sql记录
    sql游标使用
    sql触发器
    sql函数
  • 原文地址:https://www.cnblogs.com/yrxns/p/9755834.html
Copyright © 2011-2022 走看看