Selenium POM项目报错:'WebDriver'对象无'get_locator'属性
Selenium POM模型中的AttributeError问题解决
问题场景
我为Selenium项目实现了POM模型,使用以下逻辑:
使用get_name方法获取定位器名称用于日志记录:
def get_name(locator_name: str) -> str: list_of_locators = ['_XPATH', '_ID', '_CLASS_NAME', '_NAME', '_TAG_NAME'] for word in list_of_locators: locator_name = locator_name.replace(word, "") return locator_name.replace("_", " ").title()
随后编写了获取定位器的函数:
def get_locator(self, locator: str): element = None if str(locator).endswith("_XPATH"): element = self.driver.find_element(By.XPATH, locator) elif str(locator).endswith("_ACCESSIBILITY_ID"): element = self.driver.find_element(By.TAG_NAME, locator) elif str(locator).endswith("_ID"): element = self.driver.find_element(By.ID, locator) elif str(locator).endswith("_CLASS_NAME"): element = self.driver.find_element(By.CLASS_NAME, locator) elif str(locator).endswith("_NAME"): element = self.driver.find_element(By.NAME, locator) return element
我的send_text方法如下:
def send_text(self, locator: str, value: str): locator_name = get_name(locator) self.driver.get_locator(locator).send_keys(value) log.logger.info("Typing in " + str(locator_name) + " and entered the value as : " + str(value))
运行时出现错误:
locator_name = get_name(locator) > self.driver.get_locator(locator).send_keys(value) E AttributeError: 'WebDriver' object has no attribute 'get_locator'
错误原因
- 核心错误:
get_locator是你自己封装的类中的方法,并非Selenium原生WebDriver对象的方法。你错误地通过self.driver.get_locator()调用它,而WebDriver本身没有这个属性,因此触发AttributeError。 - 额外隐藏bug:
get_locator中处理_ACCESSIBILITY_ID时,错误使用了By.TAG_NAME,应该改为By.ACCESSIBILITY_ID。- 获取元素时直接传入了带后缀的完整
locator字符串(比如"USERNAME_XPATH"),而非实际的定位表达式(比如"//input[@id='username']"),这会导致元素查找失败。
修复方案
1. 修正send_text方法的调用逻辑
将self.driver.get_locator(locator)改为self.get_locator(locator),直接调用当前类的get_locator方法:
def send_text(self, locator: str, value: str): locator_name = get_name(locator) self.get_locator(locator).send_keys(value) log.logger.info(f"Typing in {locator_name} and entered the value as : {value}")
2. 修复get_locator方法的两个bug
def get_locator(self, locator: str): element = None locator_str = str(locator) # 提取实际的定位表达式(去掉后缀) if locator_str.endswith("_XPATH"): element = self.driver.find_element(By.XPATH, locator_str.replace("_XPATH", "")) elif locator_str.endswith("_ACCESSIBILITY_ID"): # 修正为正确的By类型 element = self.driver.find_element(By.ACCESSIBILITY_ID, locator_str.replace("_ACCESSIBILITY_ID", "")) elif locator_str.endswith("_ID"): element = self.driver.find_element(By.ID, locator_str.replace("_ID", "")) elif locator_str.endswith("_CLASS_NAME"): element = self.driver.find_element(By.CLASS_NAME, locator_str.replace("_CLASS_NAME", "")) elif locator_str.endswith("_NAME"): element = self.driver.find_element(By.NAME, locator_str.replace("_NAME", "")) return element
3. 补充get_name方法的覆盖范围
把_ACCESSIBILITY_ID也加入到list_of_locators中,避免日志里出现多余的后缀:
def get_name(locator_name: str) -> str: list_of_locators = ['_XPATH', '_ID', '_CLASS_NAME', '_NAME', '_TAG_NAME', '_ACCESSIBILITY_ID'] for word in list_of_locators: locator_name = locator_name.replace(word, "") return locator_name.replace("_", " ").title()
内容的提问来源于stack exchange,提问作者qatesterbecomingprogrammer
相关产品推荐
相关产品推荐

