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

来源:1-14 层级定位思想分析

夏目鲸鱼

2018-04-22

# 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()
//img.mukewang.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)


写回答

2回答

夏目鲸鱼

提问者

2018-04-23

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

0
1
Mushishi
你去找开发确认一下你们那个是不是就是一张图片。这个时候你可以通过adb命令点击
2018-04-24
共1条回复

Mushishi

2018-04-23

你好,你是不是可以通过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
共1条回复

Python主讲移动端自动化测试框架Appium

APP自动化基础知识、po模型、关键字模型、服务自动化、持续集成

1508 学习 · 1333 问题

查看课程