def is_toast_exist(self, text, timeout=10,poll_frequency=0.01):
try:
message_loc = (MobileBy.XPATH, ".//*[contains(@text,'%s')]" % text)
WebDriverWait(self,timeout,poll_frequency).until(EC.presence_of_element_located(message_loc))
return True
except:
return False #这步直接返回了False,找不到原因
# 定位toast元素内容
def get_toast_text(self, text, timeout=10,poll_frequency=0.01):
toast_loc = (MobileBy.XPATH, '//*[@class="android.widget.Toast"]')
try:
toast = WebDriverWait(self,timeout,poll_frequency).until(EC.presence_of_element_located(toast_loc))
toast_text = toast.get_attribute('text')
return toast_text
except Exception as e:
return e
self.driver.find_element(MobileBy.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.ScrollView/android.widget.LinearLayout/android.widget.GridView/android.widget.LinearLayout[3]/android.widget.ImageView').click()
if self.is_toast_exist(self):
toast_text = self.get_toast_text(self)
print(toast_text)
else:
print('不存在')
try:
message_loc = (MobileBy.XPATH, ".//*[contains(@text,'%s')]" % text)
WebDriverWait(self,timeout,poll_frequency).until(EC.presence_of_element_located(message_loc))
return True
except:
return False #这步直接返回了False,找不到原因
# 定位toast元素内容
def get_toast_text(self, text, timeout=10,poll_frequency=0.01):
toast_loc = (MobileBy.XPATH, '//*[@class="android.widget.Toast"]')
try:
toast = WebDriverWait(self,timeout,poll_frequency).until(EC.presence_of_element_located(toast_loc))
toast_text = toast.get_attribute('text')
return toast_text
except Exception as e:
return e
self.driver.find_element(MobileBy.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.ScrollView/android.widget.LinearLayout/android.widget.GridView/android.widget.LinearLayout[3]/android.widget.ImageView').click()
if self.is_toast_exist(self):
toast_text = self.get_toast_text(self)
print(toast_text)
else:
print('不存在')