1. # -*- coding: utf-8 -*-
2. # @Time : 2021/5/1
3. # @Author : 大海
4.
5. import time
6. from appium import webdriver
7. from selenium.webdriver.support.wait import WebDriverWait
8. from selenium.webdriver.support import expected_conditions as EC
9.
10. desired_capabilities = {
11. "platformName": "Android",
12. "deviceName": "127.0.0.1:62001",
13. "platformVersion": "7.1.2",
14. "appPackage": "com.jingdong.app.mall",
15. "appActivity": ".main.MainActivity"
16.
17. }
18.
19.
20. def is_toast_exist(driver, text, timeout=30, interval=0.5):
21. """
22. :param driver: 驱动
23. :param text: toast文案
24. :param timeout: 超时时长,单位s
25. :param interval: 刷新间隔时长,单位s
26. :return: True/False
27. """
28. try:
29. toast_loc = ("xpath", f".//*[contains(@text,'{text}')]")
30. WebDriverWait(driver, timeout, interval).until(EC.presence_of_element_located(toast_loc))
31. return True
32. except Exception as e:
33. print(e)
34. return False
35.
36.
37. if __name__ == '__main__':
38. driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_capabilities)
39. driver.implicitly_wait(10)
40. # 等待Activity
41. flag = driver.wait_activity('.main.MainActivity', 10)
42. print(flag)
43. # 点击同意
44. driver.find_element_by_id('com.jingdong.app.mall:id/bqd').click()
45. time.sleep(5)
46. # 返回
47. driver.back()
48.
49. text = '再次返回退出京东'
50. if is_toast_exist(driver, text):
51. toast_text = driver.find_element_by_xpath(f"//*[contains(@text,'{text}')]").text
52. print("toast_text:", toast_text)
53. else:
54. print('未定位到toast')