问题
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