selenium自动化测试遇到的问题
错误内容如下;selenium.common.exceptions.StaleElementReferenceException:
Message:
stale element reference: element is not attached to the page document
定位出错了
RE: selenium自动化测试遇到的问题
定位的元素发生变化,定位错误 当前页面没有加载定位到元素,可能元素发生变化了吧? 没有定位到啊 没有定位到元素可以尝试进行一个元素封装:
def get_element(self, selector):
"""定位元素"""
by = selector
value = selector
bys = ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']
element = None
if by in bys:
try:
if by == 'id':
element = self.driver.find_element_by_id(value)
elif by == 'name':
element = self.driver.find_element_by_name(value)
elif by == 'class':
element = self.driver.find_element_by_class_name(value)
elif by == 'tag':
element = self.driver.find_element_by_tag_name(value)
elif by == 'link':
element = self.driver.find_element_by_link_text(value)
elif by == 'plink':
element = self.driver.find_element_by_partial_link_text(value)
elif by == 'css':
element = self.driver.find_element_by_css_selector(value)
elif by == 'xpath':
element = self.driver.find_element_by_xpath(value)
log1.info('元素定位成功。定位方式:%s,使用的值:%s' % (by, value))
return element
except NoSuchElementException:
log1.error('没有定位到元素,定位方式:%s,使用的值:%s' % (by, value), exc_info=1)
else:
log1.error('元素定位方式错误,请使用id,name,class,tag,link,plink,css,xpath为定位方式参数')
页:
[1]