ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
这个可以说是被问烂的题了,判断元素存在方法有三种: 方法一,用try...except... ~~~ def is_element_exsist(driver, locator): ''' 判断元素是否存在,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' try: driver.find_element(*locator) return True except Exception as msg: print("元素%s找不到:%s" % (locator, msg)) return False if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist(driver, loc1)) ~~~ 方法二:用elements定义一组元素方法 ~~~ def is_element_exsist1(driver, locator): ''' 判断元素是否存在,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' eles = driver.find_elements(*locator) if len(eles) < 1: return False else: return True if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist1(driver, loc1)) ~~~ (强烈推荐!)方法三:结合WebDriverWait和expected\_conditions判断 ~~~ from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait def is_element_exsist2(driver, locator): ''' 结合WebDriverWait和expected_conditions判断元素是否存在, 每间隔1秒判断一次,30s超时,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' try: WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located(locator)) return True except: return False if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist2(driver, loc1)) ~~~