How to click on context menu option appears after long press on Android app using appium python?

I am trying to automate long press action followed with clicking on the static shortcuts available in context menu.

Details:

language - python

automation lib - appium

android app - Youtube

static shortcut - Subscriptions

Sample Image of what I want to click -

Updated the image with (Please refer this)

I am able to perform longpress action on the Youtube App but unable to click on the shortcut( for example - Subscriptions) available in context menu.

Below is the code :

    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    import time
    import os
    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import StaleElementReferenceException
    from appium.webdriver.common.touch_action import TouchAction
    from selenium.webdriver.common.action_chains import ActionChains


    def reopen(android_package="com.google.android.youtube", udid="udid_xxx"):
            """
            Creating a new driver object
            """
            time.sleep(2)
            desired_caps = {
                "deviceName": "MI",
                "platformName": "Android",
                "version": "13",
                "automationName": "Appium",
                "noReset": True,
                "chromedriverExecutable": os.path.join(os.getcwd(), "chromedriver.exe"),
                "showChromedriverLog": True,
                "chromeOptions": {
                    "androidPackage": android_package,
                    "w3c": False
                },
                "autoGrantPermissions": True,
                "newCommandTimeout": 3000,
                "wdaStartupRetryInterval": 20000,
                "wdaStartupRetries": 4,
                "adbExecTimeout": 20000,
                "ignoreHiddenApiPolicyError": True,
                "udid": udid
            }

            # Create web driver
            driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

            wait = WebDriverWait(driver, timeout=5)
            by = AppiumBy.XPATH
            # searching for YouTube app 
            value = "//android.widget.ImageView[@content-desc='YouTube']"
            element = wait.until(EC.element_to_be_clickable((by, value)))

            # Performing logn_press action on YouTube app
            actions = TouchAction(driver)
            actions.long_press(element).perform()
            time.sleep(5)
            # Pressing the  Subscriptions option in context menu
            tap_element = driver.find_element(AppiumBy.XPATH, "//android.widget.TextView[@text='Subscriptions']")
            #element = wait.until(EC.element_to_be_clickable((AppiumBy.XPATH, tap_element)))
            print(tap_element.is_displayed())  # output - True
            print(tap_element.text)  # output - Subscriptions
            print(tap_element.is_enabled) # output - <bound method WebElement.is_enabled of <appium.webdriver.webelement.WebElement 
                                          #  (session="e811b0a3-c59f-4042-9613-ba650fae1d21", element="00000000-0000-0a1b-ffff-ffff000019c8")>>

            tap_element.click()  # Not working 


    if __name__ == '__main__':
         reopen()

Is the element you’ve highlighted in the screenshot the correct element to click? I ask because the @text in that element is blank. Also, in this Xpath you are looking for class ‘android.widget.TextView’, but element in screenshot is ‘android.widget.LinearLayout’. I can’t tell from description or code, but maybe you are finding the wrong element, which is not clickable.

The screenshot and code do not seem to represent the same element, so the question is confusing. If screenshot is correct, adjust code to find that element instead of this other one that is not working.

@wreed
Thank you for pointing out, I have updated the correct image where the text of the element is “Subscriptions”
I observed that attribute clickable - false , does that mean it wont be clickable using automation.

Tough question, it really depends on the underlying code. I do notice that the previous element is ‘clickable’, have you tried to find and click that one? Maybe this nested element is not the right one to click.