【appium&selenium】查找元素存在的3个demo

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jq656021898/article/details/89492633

demo1:

from selenium.common.exceptions import NoSuchElementExcwption

def isElement(identifyBy,elementValue,driver):
    sleep(1)
    identifyBy = identifyBy.upper()
    flag=None
    try:
        if identifyBy == "ID":
            driver.find_element_by_id(elementValue)
        elif identifyBy == "XPATH":
            self.driver.find_element_by_xpath(elementValue)
        elif identifyBy == "CLASS":
            self.driver.find_element_by_class_name(elementValue)
        elif identifyBy == "LINK_TEXT":
            self.driver.find_element_by_link_text(elementValue)
        flag = True
    except NoSuchElementException:
        flag = False
    finally:
        return flag

demo2:

def findItem(elementValue,driver):
	source = driver.page_source()
	if el in source:
		return True
	else:
		return False
page_source()打印的结果是:
<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
<android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="" instance="0">
<android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="" instance="0">
<android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="" instance="1">
<android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="cn.usho.sosho:id/action_bar_root" instance="1">
<android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="android:id/content" instance="2">
<android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="cn.usho.sosho:id/rl_base_root_view" instance="0">
<android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="cn.usho.sosho:id/fl_base_container" instance="3">
<android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="" instance="1">
<android.widget.ImageView index="0" text="" class="android.widget.ImageView" package="cn.usho.sosho" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]" resource-id="" instance="0"/>
</android.widget.RelativeLayout>
</android.widget.FrameLayout>
</android.widget.RelativeLayout>
</android.widget.FrameLayout>
</android.widget.LinearLayout>
</android.widget.FrameLayout>
</android.widget.LinearLayout>
</android.widget.FrameLayout></hierarchy>

demo3:

// 判断元素是否可见
 def findElement(self, mOperate):
        '''
        查找元素.mOperate是字典
        operate_type:对应的操作
        element_info:元素详情
        find_type: find类型
        '''
        try:
            WebDriverWait(self.cts, common.WAIT_TIME).until(lambda x: elements_by(mOperate, self.cts))
            return True
        except selenium.common.exceptions.TimeoutException:
            return False
        except selenium.common.exceptions.NoSuchElementException:
            print("找不到数据")
            return False

   // 操作之前,需要判断元素是否存在
    def operate_element(self,  mOperate):
        if self.findElement(mOperate):
            elements = {
                common.CLICK: lambda: operate_click(mOperate, self.cts),
                # common.TAP: lambda: operate_tap(mOperate["find_type"], self.cts,  mOperate["element_info"], arg),
                common.SEND_KEYS: lambda: send_keys(mOperate, self.cts),
                common.SWIPELEFT: lambda : opreate_swipe_left(mOperate, self.cts)
            }
            return elements[mOperate["operate_type"]]()
        return False
展开阅读全文

没有更多推荐了,返回首页