如何在Python中的Appium中找到具有相同ID的多个元素?

时间:2019-10-17 13:09:35

标签: appium appium-android appium-ios python-appium appium-desktop

我正在用python在Andriod项目的Appium中编写测试代码。问题是我无法访问具有相同ID的两个不同Activity中的两个按钮。我试图以这种方式访问​​第二个按钮,但是它们都不起作用。该如何解决呢? driver.find_element_by_id("com.myapp.testApp:id/login[1]").click(), driver.find_element_by_class_name("android.widget.Button").click() driver.find_element_by_xpath("(//button[@id='login'])[1]").click() driver.find_element_by_xpath("//android.widget.Button[@text='Change Password']").click()

4 个答案:

答案 0 :(得分:0)

您可以尝试使用textview:

driver.find_element_by_xpath("//android.widget.TextView[@text='Change Password']").click()

答案 1 :(得分:0)

使用.find_elements*

elements = driver.find_elements_by_xpath("xpath")
#check elements number
print(len(elements))
#click second element
elements[1].click()

答案 2 :(得分:0)

如果按钮文本不同,请尝试使用accessibility_id

driver.find_element_by_accessibility_id("Login").click()

driver.find_element_by_accessibility_id("Change Password").click()

OR

如果您熟悉uiautomator,

driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.myapp.testApp:id/login[1]")')

driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.myapp.testApp:id/login[2]")')

答案 3 :(得分:0)

您可以将get方法与findElements一起使用:

driver.findElements(By.xpath("yourXpath]")).get(0).click();