想要通过提供 xpath 来使用 appium 点击 android 元素

Posted

技术标签:

【中文标题】想要通过提供 xpath 来使用 appium 点击 android 元素【英文标题】:Wanted to click on android element using appium by providing xpath 【发布时间】:2019-06-30 02:23:36 【问题描述】:

我有一个按钮 onclick 事件,但是,当它运行时,实际上并没有实际单击该按钮。当我执行 .isDisplayed() 时,总是会找到该元素。但是,测试仍然失败。 在这里,我想单击 Not now 按钮,但在以下代码中不起作用, 我该怎么办?

     WebElement c=driver.findElement(By.xpath("//android.widget.Button[@resource-id='android:id/button2']"));
            if(c.isDisplayed())
            
                c.click();
            
    Thread.sleep(20000);


    driver.findElement(By.xpath("//android.widget.EditText[@text='Email']")).sendKeys("testappium@gmail.com");

我也尝试了以下代码,但我没有工作

driver.findElement(By.xpath("//android.widget.FrameLayout[@bounds='[27,724][1053,1267]']/android.widget.FrameLayout[@bounds='[75,772][1005,1219]']/android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.LinearLayout[@resource-id='android:id/parentPanel']/android.widget.LinearLayout[@resource-id='android:id/buttonPanel']/android.widget.Button[@bounds='[517,1063][777,1207]']")).click();

还尝试了以下代码:

driver.findElement(By.xpath("//*[@class='android.widget.Button' and @bounds='[517,1063][777,1207]']")).click();

我得到的错误是:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html

*** Element info: Using=xpath, value=//android.widget.EditText[@text='Email']

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:152)
    at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at execution.AppTest.main(AppTest.java:183)

UI 自动化查看器:

【问题讨论】:

你能发布日志吗? 【参考方案1】:

这里可以直接使用id。

WebElement element=driver.findElement(By.xpath("//android.widget.Button[@text='Not Now']"));
if(element.isDisplayed())
    element.click();

【讨论】:

我已经编辑了答案,如果它现在适合你,请检查 xpath。 @PrachiDalvi 请让我知道更新的答案有效:)【参考方案2】:

检查以下 XPath 有多少匹配项

String xPath = "//*[@resource-id='android:id/button2']";

如果有多个匹配项,则在下面的 XPath 中传递匹配的索引号:

String xPath = "(//*[@resource-id='android:id/button2'])[matching index number]";

在搜索一个元素或执行任何操作之前给一些延迟。试试下面的代码:

Thread.sleep(3000);
String xPath = "//*[@resource-id='android:id/button2']";
List<WebElement> notNow = driver.findElements(By.xpath(xPath));
if(notNow.size() > 0) 
    notNow.get(0).click(); // You can pass the matching index in get() also if there are multiple matches

或者你可以使用下面的 XPath,在尝试查找之前给一些延迟:

String xPath = "//android.widget.TextView[@text='Text That You Want to locate']"

希望对你有帮助……

【讨论】:

【参考方案3】:

你可以这样使用

     WebElement element=driver.findElement(By.xpath("//android.widget.Button[@id='button2']"));
        if(element.isDisplayed())
        
            element.click();
        

string elementId = driver.findElement(By.Id("button2"));
if(elementId.isDisplayed) element.click(); 

两者都应该工作

【讨论】:

【参考方案4】:

不建议在 appium 中使用 xpath。

您可以像

一样使用 findElementById
driver.findElementById("android:id/button2").click()

【讨论】:

在多次单击(循环中)的情况下不起作用,知道吗?

以上是关于想要通过提供 xpath 来使用 appium 点击 android 元素的主要内容,如果未能解决你的问题,请参考以下文章

Appium利用xpath查找同级节点方法

Appium:使用 XPath 通过部分 elementId/UID 查找元素

用于在 android 中列出元素的 xPath(使用 appium 自动化)

四:RF框架appium工具之xpath定位

如何通过移动 xpath 与 Appium 匹配 @text 属性和正则表达式(正则表达式)来查找元素?

Appium根据xpath获取控件实例随笔