加元素是否存在的判断条件
Common_methods.py文件from selenium.webdriver.support import expected_conditions as EC def check_element_exists(driver,element,condition): try: if condition=='class': driver.find_element_by_class_name(element) elif condition=='id': driver.find_element_by_class_id(element) elif condition=='xpath': driver.find_element_by_class_xpath(element) return True except Exception as e: return False
另一个文件引用: if Common_methods.check_element_exists(self.driver,"//*[@id=\"111\"]",'xpath'): self.driver.find_element_by_class_xpath("//*[@id=\"111\"]").click()