您的代码封装的函数已经可以实现重复调用,并且在元素不存在时不会报错。但是需要注意的是,函数返回值应该是布尔类型,而不是 1 和 0。以下是修改后的代码:\n\npython\nelement_detail_edit_btn = '*[@id="ctl00_ctl00_ActionButtons_ActionButtons_Toolbar_btnOwnAndEdit"]' \nOWT_EDIT_ELEMENT = '//*[@id="ctl00_ctl00_ActionButtons_ActionButtons_Toolbar_lbtnEdit"]' \n\ndef is_element_exist(driver, xpath):\n try:\n driver.find_element(By.XPATH, xpath)\n return True\n except NoSuchElementException:\n return False\n\nif is_element_exist(self.driver, element.element_detail_edit_btn):\n self.driver.find_element(By.XPATH, element.element_detail_edit_btn).click()\nelif is_element_exist(self.driver, OWT_EDIT_ELEMENT):\n self.driver.find_element(By.XPATH, OWT_EDIT_ELEMENT).click()\n\n\n现在,您可以通过调用 is_element_exist 函数并传入元素的 XPath 坐标来判断元素是否存在,从而避免错误报错。


原文地址: https://www.cveoy.top/t/topic/pQIJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录