piaoquan_tv_v1.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # @Author: luojunhui
  3. # @Time: 2023/12/14
  4. import time
  5. from appium import webdriver
  6. from appium.webdriver.extensions.android.nativekey import AndroidKey
  7. from selenium.webdriver.common.by import By
  8. class PQuanTv(object):
  9. def __init__(self):
  10. self.platform = "票圈|3亿人喜欢的视频平台"
  11. self.caps = {
  12. "platformName": "Android",
  13. "deviceName": "Android",
  14. "appPackage": "com.tencent.mm",
  15. "appActivity": ".ui.LauncherUI",
  16. "autoGrantPermissions": True,
  17. "unicodekeyboard": True,
  18. "resetkeyboard": True,
  19. "noReset": True,
  20. "recreateChromeDriverSessions": True,
  21. "printPageSourceOnFailure": True,
  22. "newCommandTimeout": 6000,
  23. "automationName": "UiAutomator2",
  24. "showChromedriverLog": True,
  25. "chromeOptions": {"androidProcess": "com.tencent.mm:tools"},
  26. "enableWebviewDetailsCollection": True,
  27. "setWebContentsDebuggingEnabled": True,
  28. "chromedriverExecutable": "/usr/bin/chromedriver",
  29. }
  30. self.driver = webdriver.Remote("http://localhost:4750/wd/hub", self.caps)
  31. class PQuanTvDev(object):
  32. """
  33. __init__: 初始化类, 初始化driver;
  34. open_minigram: 下拉,通过搜索小程序名字打开小程序
  35. open_chat_box: 进入微信之后打开聊天框
  36. click_ad: 点击消息,打开小程序之后,点击广告, 退出
  37. """
  38. def __init__(self, rule_dict):
  39. self.platform = "票圈|3亿人喜欢的视频平台"
  40. self.rule_dict = rule_dict
  41. self.caps = {
  42. "platformName": "Android",
  43. "deviceName": "Android",
  44. "appPackage": "com.tencent.mm",
  45. "appActivity": ".ui.LauncherUI",
  46. "autoGrantPermissions": True,
  47. "unicodekeyboard": True,
  48. "resetkeyboard": True,
  49. "noReset": True,
  50. "recreateChromeDriverSessions": True,
  51. "printPageSourceOnFailure": True,
  52. "newCommandTimeout": 6000,
  53. "automationName": "UiAutomator2",
  54. "showChromedriverLog": True,
  55. "chromeOptions": {"androidProcess": "com.tencent.mm:tools"},
  56. "enableWebviewDetailsCollection": True,
  57. "setWebContentsDebuggingEnabled": True,
  58. "chromedriverExecutable": "/usr/bin/chromedriver",
  59. }
  60. self.driver = webdriver.Remote("http://localhost:4750/wd/hub", self.caps)
  61. def open_minigram(self):
  62. self.driver.implicitly_wait(5)
  63. for i in range(10):
  64. if self.driver.find_elements(By.ID, "com.tencent.mm:id/f2s"):
  65. break
  66. elif self.driver.find_element(By.ID, "com.android.systemui:id/dismiss_view"):
  67. self.driver.find_element(By.ID, "com.android.system:id/dismiss_view").click()
  68. else:
  69. pass
  70. size = self.driver.get_window_size()
  71. self.driver.swipe(
  72. int(size["width"] * 0.5),
  73. int(size["height"] * 0.2),
  74. int(size["width"] * 0.5),
  75. int(size["height"] * 0.8),
  76. 200,
  77. )
  78. self.driver.find_elements(By.XPATH, '//*[@text="{}"]'.format(self.platform))[-1].click()
  79. def open_chat_box(self):
  80. self.driver.implicitly_wait(10)
  81. print("start open chatbox")
  82. box_list = self.driver.find_elements(By.ID, "com.tencent.mm:id/hg4")
  83. print(box_list)
  84. if box_list:
  85. box_list[0].click()
  86. print("finish open chatbox")
  87. def click_ad(self):
  88. minigram = self.driver.find_elements(By.ID, "com.tencent.mm:id/b2o")
  89. if minigram:
  90. minigram[0].click()
  91. print("成功打开小程序")
  92. self.driver.press_keycode(AndroidKey.BACK)
  93. else:
  94. return
  95. if __name__ == "__main__":
  96. Pv = PQuanTvDev(rule_dict={})
  97. print("成功切换到webview")
  98. Pv.open_chat_box()
  99. time.sleep(10)
  100. Pv.click_ad()