Search code examples
pythonappiumappium-android

how to click a button if another element contains a text


I am testing an app and have a difficulty to click a button which another element contains a text as seen below.

enter image description here

I want to click on "DETAILS" button which has "UPCOMING" word above it.

I have tried to do it but none of it does the job.

First

for booking_card in context.driver.find_element_by_xpath("//*(@class='android.view.ViewGroup')"):
        if booking_card.text == "UPCOMING":
            context.driver.find_element_by_xpath("//*[contains(@text, 'DETAILS')]").click()

Second

context.driver.find_element_by_xpath("//*[contains(@text, 'UPCOMING')]//*[@text='DETAILS']").click()

Third I tried to find the details button using the parent, sibling and ancestor method but still failed.

//*[@class='android.view.ViewGroup']//*[contains(@text, 'UPCOMING')]/following-sibling::*[contains(@text, 'DETAILS')]

here is the Hierarchy of the elements: enter image description here

I really appreciate any help, thank you. If there's something else I need to explain, let me know.


Solution

  • I have discovered the answer, so the way to do it is to use parent, following-sibling method. all I did is to make sure the hierarchy of the elements are correct so that I can find what I was looking for.

    here's the code that I have done, it's simple.

    isPresent(context, MobileBy.XPATH, "//*[@text='UPCOMING']/parent::*[@class='android.view.ViewGroup']/parent::*[@class='android.view.ViewGroup']/following-sibling::*[@class='android.view.ViewGroup']//*[@text='DETAILS']").click()
    

    so the way I did it, to look the 'UPCOMING' word first, and then I need to find the parent element and then click on the DETAILS button.