Android 真机 无法获取android.view.View原生控件 元素

androidDriver.findElement(By.linkText(“立即购买”));
androidDriver.findElement(By.xpath("//com.uc.webview.export.WebView[@content-desc=“WVUCWebView”]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View[13]/android.view.View[2]/android.view.View/android.view.View[2]/android.view.View/android.view.View/android.view.View[2]/android.view.View/android.view.View[2]/android.view.View[1]"));
androidDriver.findElementByAndroidUIAutomator(“new UiSelector().text(“立即购买”)”);
以上三种获取元素都失败
报错:An element could not be located on the page using the given search parameters.
原因:Android.view.View是原生控件,所以无法获取!求大神帮助怎么解决


源码:
源码.txt (155.9 KB)

share page source.

// Java
System.out.println(driver.getPageSource());

this is webView. it will much easier if you switch to WevView (ask developers to make it debugabble in app) and continue like in pure web :slight_smile:

you see android.webkit.WebView = webView

感谢您的回答,我在自学‘app’自动化测试,随便找的‘app’进行测试学习,我不是这家公司的,不能要求他们开发人员在应用程序中使其可调试。android.view.View 无法获取元素问题,我在谷歌,百度上都没找到解决办法,所以才在这里发帖,看看自己是否有漏掉知识点没学到!

Ok lets make it a bit more step by step check. Looks like your search has wrong syntax. Missing \

        String txt = "立即购买";
        WebElement el = driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"" + txt + "\")"));
        boolean isFound = false;
        try {
            isFound = !((RemoteWebElement) el).getId().isEmpty();
        } catch (Exception e) {
        }
        if (isFound) { // we found element
            Rectangle rectangle = el.getRect();
            System.out.println("center: " + (rectangle.x + rectangle.width / 2) + ", " + (rectangle.y + rectangle.height / 2));
        }

是我的查找元素方法出了问题,感谢您提供的代码,让我找到了解决bug的方法

看到输出的坐标,我使用了移动查找,能找到元素,点击按钮也不会报错!:pray:感谢!

oi here is error. should be:

System.out.println("center: " + (rectangle.x + rectangle.width / 2) + ", " + (rectangle.y + rectangle.height / 2));

Also to use new UiScrollable(new UiSelector() .... you should have ScrollView in your page source. While you do not have it. So it will not work.