簡體   English   中英

如何使用循環在appium中滑動直到找到元素

[英]How to swipe till element is found in appium with using loop

我可以滑動直到找到元素,但隨后出現 NoSuchElementException。

我寫了以下代碼。

int startX=(int) (dimension.width*0.5);
        int startY=(int) (dimension.height*0.8);

        int endX=(int) (dimension.width*0.2);
        int endY=(int) (dimension.width*0.2);

        TouchAction touch=new TouchAction(driver);
        boolean b=false;
        while (true) {
            touch.press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
            .moveTo(PointOption.point(endX, endY)).release().perform();
            Thread.sleep(5000);
            try {
                b=driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).isDisplayed();
                if(b) {
                    b=true;
                    break;
                }
                else {
                    continue;
                }
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).click();

請幫忙。 謝謝在此處輸入圖片說明

使用基於 UIAutomator 的定位器會更可靠,因為您顯然有一個可滾動的視圖:

MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
        "new UiScrollable(new UiSelector().scrollable(true))" +
         ".scrollIntoView(new UiSelector().textContains(\"Sweep\"))"));

它不僅會滾動到元素,還會確保它在視圖端口區域內並且能夠進行交互。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM