Appium is unable to find button on android device

你。 提交于 2020-01-25 06:52:48

问题


The following is the html code for the button that I'm trying to click.

 <Button rounded style={styles.pickBtn} title="get started" onPress={signIn} testID="completeBoarding">
            <Text style={styles.pickBtnText}>GET STARTED</Text>
          </Button>

This is the code that I have tried to use to click on said button.

String xPath = "//button[normalize-space()='GET STARTED']";
    AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
            ExpectedConditions.elementToBeClickable(MobileBy.xpath(xPath)));
    searchElement.click();

-

AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
            ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("get started")));
    searchElement.click();

-

AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
            ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("GET STARTED")));
    searchElement.click();

From the html provided above, what is the correct way to click the button? Neither of the above methods I tried ran successfully. Each failed with an error message saying that the element could not be found.


回答1:


Please try with below code to click.

String xPath = "//*[@title='get started']";
    AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
            ExpectedConditions.elementToBeClickable(MobileBy.xpath(xPath)));
    searchElement.click();

Tap by using text:

You can tap using text with UiAutomator2 Add in desired capability UiAutomator2 if you are using appium as automation engine.

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

then use this function

public void TapByText(String buttonText) {
        String buttonName="new UiSelector().text(\"MY_TEXT\")".replace("MY_TEXT", buttonText);
        @SuppressWarnings("unchecked")
        List<WebElement> el = (List<WebElement>) driver.findElements(MobileBy.AndroidUIAutomator(buttonName));
        System.out.println(el.size());
        new TouchAction(driver).press(ElementOption.element(el.get(0))).waitAction().release().perform();

    }


来源:https://stackoverflow.com/questions/59814868/appium-is-unable-to-find-button-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!