美文网首页
appium之判断元素是否存在(922)

appium之判断元素是否存在(922)

作者: Qin0821 | 来源:发表于2018-08-08 17:27 被阅读0次

使用场景

app在实际使用中会针对各种情况弹许多框,比如更新。
为了不让测试脚本报错,需要对是否有弹框做一个判断。

解决方法

def isElement(self,identifyBy,c):
        '''
        Determine whether elements exist
        Usage:
        isElement(By.XPATH,"//a")
        ''' 
        time.sleep(1)
        flag=None
        try:
            if identifyBy == "id":
                #self.driver.implicitly_wait(60)
                self.driver.find_element_by_id(c)
            elif identifyBy == "xpath":
                #self.driver.implicitly_wait(60)
                self.driver.find_element_by_xpath(c)
            elif identifyBy == "class":
                self.driver.find_element_by_class_name(c)
            elif identifyBy == "link text":
                self.driver.find_element_by_link_text(c)
            elif identifyBy == "partial link text":
                self.driver.find_element_by_partial_link_text(c)
            elif identifyBy == "name":
                self.driver.find_element_by_name(c)
            elif identifyBy == "tag name":
                self.driver.find_element_by_tag_name(c)
            elif identifyBy == "css selector":
                self.driver.find_element_by_css_selector(c)
            flag = True
        except NoSuchElementException,e:
            flag = False
        finally:
            return flag

判断是否有更新框示例:

while True:
    update = self.isElement(By.CSS_SELECTOR, "body > div.update-app-modal.small > div > div.update-btn-box > div.small-version")
    if update:
        print ("have update")
        self.driver.find_element_by_css_selector("body > div.update-app-modal.small > div > div.update-btn-box > div.small-version").click()
    else:
        break

相关文章

网友评论

      本文标题:appium之判断元素是否存在(922)

      本文链接:https://www.haomeiwen.com/subject/jpqhvftx.html