TA的每日心情 | 慵懒 2021-3-18 16:15 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]测试小兵
|
没有定位到元素
可以尝试进行一个元素封装:
- def get_element(self, selector):
- """定位元素"""
- by = selector[0]
- value = selector[1]
- 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为定位方式参数')
复制代码
|
|