Browse Source

add weixinzhishu: search_key

wangkun 2 years ago
parent
commit
7cc75097ab
1 changed files with 31 additions and 5 deletions
  1. 31 5
      weixinzhishu/weixinzhishu_main/search_key.py

+ 31 - 5
weixinzhishu/weixinzhishu_main/search_key.py

@@ -21,6 +21,8 @@ import time
 
 import psutil
 from appium import webdriver
+from pyasn1.type.univ import Integer
+from selenium.common import NoSuchElementException, WebDriverException
 from selenium.webdriver.common.by import By
 sys.path.append(os.getcwd())
 from common.common import Common
@@ -40,16 +42,40 @@ class Searchkey:
             # Common.logger(log_type, crawler).info('点击微信指数')
             driver.find_elements(By.NAME, '消息')[-1].click()
             time.sleep(2)
-            cls.kill_pid(log_type, crawler)
-            time.sleep(2)
+            # cls.kill_pid(log_type, crawler)
+            # time.sleep(2)
             driver.quit()
-            time.sleep(2)
-            windowHandles = driver.getWindowHandles()
-            print(windowHandles)
+            # time.sleep(2)
+            # windowHandles = driver.getWindowHandles()
+            print('关闭微信指数')
+            new_driver = cls.close_weixinzhishu()
+            new_driver.find_elements(By.NAME, '关闭')[-1].click()
+            # print(windowHandles)
             # cls.rmtree_WMPFRuntime()
         except Exception as e:
             Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
 
+    @classmethod
+    def close_weixinzhishu(cls, app_name='微信指数'):
+        """
+        *通过名字找到windowsdriver
+        *通过窗口名称,从桌面对象获取webdriver对象
+        """
+        new_caps = {'app': "Root"}
+        try:
+            new_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=new_caps)
+            windowElement = new_driver.find_elements(By.NAME, app_name)
+            if len(windowElement) != 0:
+                newWindowHandle = windowElement[0].get_attribute("NativeWindowHandle")
+                app_caps = {"appTopLevelWindow": newWindowHandle}
+                app_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
+                                              desired_capabilities=app_caps)
+                return app_driver
+        except NoSuchElementException:
+            print(f"没有找到对应的名字的窗口:{app_name}")
+        except WebDriverException:
+            print("连接winappdriver失败,请检查是否启动")
+
     @classmethod
     def kill_pid(cls, log_type, crawler):
         try: