在Appium中,如果按钮位于视图内,无法直接进行检测和点击。解决此问题的一种方法是通过使用UI Automator Viewer工具来查看应用程序中的控件元素。然后可以使用xpath或其他识别方法定位该按钮并进行点击操作。
以下是一个基于xpath的示例代码:
# 导入selenium模块和WebDriverWait类
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
# 启动Appium服务
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9.0'
desired_caps['deviceName'] = 'device'
desired_caps['appPackage'] = 'com.example.app'
desired_caps['appActivity'] = 'com.example.app.MainActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 等待视图加载完成
wait = WebDriverWait(driver, 10)
# 通过xpath定位按钮并点击
button = wait.until(lambda x: x.find_element_by_xpath("//android.view.View/android.widget.Button[@text='Button Text']"))
button.click()
# 关闭Appium服务
driver.quit()
需要注意的是,xpath表达式应该根据应用程序中实际控件元素进行调整。