Appium 解決微信公眾號、小程序切換 webview 后無法定位元素的問題


如何切換webview進入小程序請參考https://testerhome.com/topics/12003

腳本思路:
進入webview后會存在多個handle同Web頁簽一樣,獲取所有的handle,然后在遍歷所有的handle,通過switch_to_window進行handle切換,當某個handle可以定位到我們需要定位的元素時,然后我們就可以進行之后的自動化操作了!

  1.  
    <code style= "font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;line-height:18px;font-size:12px;color:rgb(68,68,68);border:none;">#coding=utf-8
  2.  
    from appium import webdriver
  3.  
    import time,os,re
  4.  
    from appium.webdriver.common.touch_action import TouchAction
  5.   
  6.   
  7.  
    desired_caps = {}
  8.  
    desired_caps[ 'platformName'] = 'Android'
  9.  
    desired_caps[ 'deviceName'] = 'Android001'
  10.  
    desired_caps[ 'unicodeKeyboard'] = True
  11.  
    desired_caps[ "resetKeyboard"] = True
  12.  
    desired_caps[ "newCommandTimeout"]=30
  13.  
    desired_caps[ 'fullReset'] = 'false'
  14.  
    desired_caps[ 'appPackage'] = 'com.tencent.mm'
  15.  
    desired_caps[ 'appActivity'] = 'com.tencent.mm.ui.LauncherUI'
  16.  
    desired_caps[ 'recreateChromeDriverSessions'] = True
  17.  
    desired_caps[ 'noReset'] = True
  18.  
    desired_caps[ 'newCommandTimeout'] = 10
  19.  
    desired_caps[ 'chromeOptions']={'androidProcess': 'com.tencent.mm:appbrand0'}
  20.  
    driver = webdriver.Remote(command_executor = 'http://127.0.0.1:4723/wd/hub',desired_capabilities = desired_caps)
  21.  
    time.sleep( 2)
  22.  
    time.sleep( 1)
  23.  
    driver.implicitly_wait( 10)
  24.  
    driver.find_element_by_name( '發現').click()
  25.  
    time.sleep( 1)
  26.  
    driver.swipe( 100,1200,100,900)
  27.  
    driver.find_element_by_name( '小程序').click()
  28.  
    driver.find_element_by_name( '美團外賣').click()
  29.  
    time.sleep( 2)
  30.  
    contexts = driver.contexts
  31.  
    print contexts
  32.  
    time.sleep( 2)
  33.  
    driver.switch_to.context( 'WEBVIEW_com.tencent.mm:appbrand0')
  34.  
    print '切換成功'
  35.  
    print driver.current_context
  36.  
    all_handles = driver.window_handles
  37.  
    print len(all_handles)
  38.  
    for handle in all_handles:
  39.  
    try:
  40.  
    driver.switch_to_window(handle)
  41.  
    print driver.page_source
  42.  
    driver.find_element_by_css_selector( '.filter-select.flex-center') #定位“篩選 ”按鈕
  43.  
    print '定位成功'
  44.  
    break
  45.  
    except Exception as e:
  46.  
    print e
  47.  
    driver.find_element_by_css_selector( '.filter-select.flex-center').click()
  48.  
    time.sleep( 5)
  49.  
    driver.quit()</code>

原本:https://blog.csdn.net/qq_35741999/article/details/79430077


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM