请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

一个webview的元素属性只知道content-desc,那么怎么定位?

# coding=utf-8
from appium import webdriver
from time import sleep


def get_driver():
   capabilities = {
     "platformName": "android",
     "deviceName": "192.168.56.101:5555",
     "app": "/Users/francis/Projects/Appium/app_package/release.apk_1.2.1.apk",
     # "appActivity": "com.rootoken.wallet.MainActivity",
     "noReset": "true"
   }

   driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities)
   sleep(2)
   return driver


def window_handle():
   # 窗口切换到新打开的窗口
   c_window = driver.current_context
   print(c_window)
   all_handles = driver.contexts
   for handle in all_handles:
       print(handle)
       if 'WEBVIEW' in handle:
           driver.switch_to.context(handle)
           print('跳转到webview模式')
           print('跳转当前窗口句柄:' + handle)
           break


def get_size():
   size = driver.get_window_size()
   width = size['width']
   height = size['height']
   return width, height


def swipe_left():
   size = get_size()
   x1 = size[0]*9/10
   y1 = size[1]/2
   x = size[0]/10
   y = y1
   driver.swipe(x1, y1, x, y)


def swipe_right():
   size = get_size()
   x1 = size[0]/10
   y1 = size[1]/2
   x = size[0]*9/10
   y = y1
   driver.swipe(x1, y1, x, y)


def swipe_up():
   size = get_size()
   x1 = size[0]/2
   y1 = size[1]*9/10
   x = x1
   y = size[1]/10
   driver.swipe(x1, y1, x, y)


def swipe_down():
   size = get_size()
   x1 = size[0]/2
   y1 = size[1]/10
   x = x1
   y = size[1]*9/10
   driver.swipe(x1, y1, x, y)


def swipe_on(direction):
   if direction == 'up':
       swipe_up()
   elif direction == 'down':
       swipe_down()
   elif direction == 'left':
       swipe_left()
   else:
       swipe_right()


driver = get_driver()
# # language_choice = '//android.widget.Button[contains(@content-desc, "简体中文")]'
# language_choice = '简体中文'
# sleep(3)
# driver.find_element_by_accessibility_id(language_choice).click()

sleep(3)
swipe_on('left')
swipe_on('left')
swipe_on('left')
sleep(2)

window_handle()

# step_in = driver.find_elements_by_class_name('android.widget.Button')
# step_in[1].click()

route_in = "//android.widget.Button[contains(@content-desc, '进入')]"
driver.find_element_by_xpath(route_in).click()
//img1.sycdn.imooc.com//szimg/5adc8f5b0001e46215721156.jpg


日志:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//android.widget.Button[contains(@content-desc, '进入')]"}

  (Session info: chrome=51.0.2704.91)

  (Driver info: chromedriver=2.23.409710 (0c4084804897ac45b5ff65a690ec6583b97225c0),platform=Mac OS X 10.11.6 x86_64)


正在回答 回答被采纳积分+3

2回答

提问者 夏目鲸鱼 2018-04-23 12:15:45

list的方式拿到的元素个数总数为0,很奇怪

0 回复 有任何疑惑可以回复我~
  • 你去找开发确认一下你们那个是不是就是一张图片。这个时候你可以通过adb命令点击
    回复 有任何疑惑可以回复我~ 2018-04-24 10:37:08
Mushishi 2018-04-23 11:35:58

你好,你是不是可以通过list进行定位,然后按照index去呢

0 回复 有任何疑惑可以回复我~
  • 提问者 夏目鲸鱼 #1
    尝试过用以下两种方式,但是得到的列表都是空列表,这个很奇怪的。
    # step_in = driver.find_elements_by_class_name('android.widget.Button')
    # step_in[1].click()
    
    find_elements_by_xpath:
    route_in = "//android.widget.Button[contains(@content-desc, '进入')]"
    回复 有任何疑惑可以回复我~ 2018-04-23 14:17:43
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信