如何使用Appium for python为存储在数组中的元素找到同级元素



我使用以下方法声明了一个元素数组:

checkboxes = self.driver.find_elements(AppiumBy.XPATH, '//XCUIElementTypeButton[@name="Square"]')

如何找到数组中第一个元素的同级元素?

这个

siblingElement = checkboxes[0].find_element(by=AppiumBy.XPATH, value='./following-sibling::XCUIElementTypeStaticText')

失败,返回以下错误消息:

NoSuchElementError: An element could not be located on the page using the given search parameters.

我试着使用这个文档:

https://appium.io/docs/en/commands/element/find-elements/

https://appium.io/docs/en/commands/element/find-element/

请注意,我在框架中使用Python。

UI的域:

<XCUIElementTypeButton type="XCUIElementTypeButton" name="Square" label="Square" enabled="true" visible="true" accessible="true" x="15" y="428" width="20" height="21" index="22"/>
<XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="checkbox 1 text" name="checkbox 1 text" label="checkbox 1 text" enabled="true" visible="true" accessible="true" x="43" y="428" width="308" height="18" index="23"/>
<XCUIElementTypeButton type="XCUIElementTypeButton" name="Square" label="Square" enabled="true" visible="true" accessible="true" x="15" y="478" width="20" height="21" index="24"/>
<XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="checkbox 2 text" name="checkbox 2 text" label="checkbox 2 text" enabled="true" visible="true" accessible="true" x="43" y="478" width="260" height="35" index="25"/>
<XCUIElementTypeButton type="XCUIElementTypeButton" name="Square" label="Square" enabled="true" visible="true" accessible="true" x="15" y="542" width="20" height="21" index="26"/>
<XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="checkbox 3 text" name="checkbox 3 text" label="checkbox 3 text" enabled="true" visible="true" accessible="true" x="43" y="542" width="333" height="86" index="27"/>
<XCUIElementTypeButton type="XCUIElementTypeButton" name="Square" label="Square" enabled="true" visible="true" accessible="true" x="15" y="657" width="20" height="21" index="28"/>
<XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="checkbox 4 text" name="checkbox 4 text" label="checkbox 4 text" enabled="true" visible="true" accessible="true" x="43" y="657" width="320" height="52" index="29"/>

第一种情况

def tap_by_index(self, locator_string, index):
locator = self.get_locator(locator_string)
if str(locator_string[0]).startswith("xpath"):
self.driver.find_elements(AppiumBy.XPATH, str(locator))[index].click()
def get_locator(locator):
exploited_locator = locator[1]
return exploited_locator


第二种情况

def tap_by_index(self, locator: Tuple[By, str], i: int):
"""i: element index"""
self.wait(locator) # WAIT UNTIL VISIBLE
elements = self.driver.find_elements(*locator)
elements[i].click()