无法在Android设备上向下滚动页面

时间:2016-07-15 13:33:22

标签: java android selenium appium

我无法使用appium在Android设备上的移动页面中向下滚动。我已经尝试过driver.scrollTo(element),它没有向下滚动到指定的元素。 我尝试过使用Actions类但是因为“方法尚未实现”而出现错误 我尝试过使用Javascript的另一种方法,但它也没有用。 有人提出建议。

以下是代码:

public static void main(String[] args) throws IOException, InterruptedException {
        DesiredCapabilities cap = new DesiredCapabilities();

        cap.setCapability(CapabilityType.BROWSER_NAME, "Android"); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
        cap.setCapability(CapabilityType.VERSION, "6.0.1");
        cap.setCapability(CapabilityType.PLATFORM, "Mac");
        cap.setCapability("platformName", "android");
        cap.setCapability("platformVersion", "6.0.1");
        cap.setCapability("deviceName", "4d00a89e4b2631e9");
        cap.setCapability("app", "/Users/ds_nivedha/Downloads/flipkart.apk");
        cap.setCapability("appPackage", "com.usablenet.mobile.walgreen"); //Replace with your app's package
        cap.setCapability("appActivity", "com.usablenet.mobile.walgreen.AppStart"); //Replace with app's Activity
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

        /* MobileElement skip= driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.Button[1]"));
        skip.click(); */

         MobileElement skiparrow=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[3]/android.widget.ImageButton[1]"));
        skiparrow.click(); 

        MobileElement skiparrowRefill=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.ImageButton[1]"));

        skiparrowRefill.click();

        MobileElement skiparrowPaperless=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.ImageButton[1]"));

        skiparrowPaperless.click();

        MobileElement done=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]"));
        done.click();



        MobileElement shopProducts=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.TextView[2]"));
        shopProducts.click();

        MobileElement personalCare=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.GridView[1]/android.widget.TextView[2]"));
        personalCare.click();


        MobileElement hairCare=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ListView[1]/android.widget.TextView[3]"));
        hairCare.click();


        MobileElement stylingProduct=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ListView[1]/android.widget.TextView[3]"));
        stylingProduct.click();

        MobileElement faroukAddToCart=
        driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[3]/android.view.View[2]/android.view.View[1]/android.view.View[3]/android.view.View[1]/android.view.View[1]/android.view.View[4]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.Button[1]"));
        faroukAddToCart.click();

        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//android.widget.Button[contains(@resource-id,'addToCart-cart-checkout')]")));

        driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'addToCart-cart-checkout')]")).click();

        //driver.scrollTo("Proceed to checkout");

        MobileElement proceedToCheckout=
                driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'proceedtocheckout')]"));


        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", proceedToCheckout);
        Thread.sleep(500); 

        /* Actions actions = new Actions(driver);
        actions.moveToElement(proceedToCheckout);
        actions.perform(); */

        MobileElement checkout=
                driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[2]/android.view.View[5]/android.view.View[2]/android.view.View[5]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Button[1]"));
        checkout.click();

2 个答案:

答案 0 :(得分:0)

使用滑动方法 保持开始x和结束x值不变并更改开始y和结束y值以向上或向下滚动

答案 1 :(得分:0)

scrollTo()方法现已弃用,您可以在官方网站上查看here

相反,如果您可以使用swipe方法在移动设备上垂直滑动并检查您的预期文字是否可见,那么您可以进行更多滑动,当然您将使用循环这样做。 要详细了解Swipe,请参阅here

对于Swipe方法,请参阅以下示例

 public void swipingVertical() throws InterruptedException {
  //Get the size of screen.
  size = driver.manage().window().getSize();
  System.out.println(size);

  //Find swipe start and end point from screen's with and height.
  //Find starty point which is at bottom side of screen.
  int starty = (int) (size.height * 0.80);
  //Find endy point which is at top side of screen.
  int endy = (int) (size.height * 0.20);
  //Find horizontal point where you wants to swipe. It is in middle of screen width.
  int startx = size.width / 2;
  System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);

  //Swipe from Bottom to Top.
  driver.swipe(startx, starty, startx, endy, 3000);
  Thread.sleep(2000);
  //Swipe from Top to Bottom.
  driver.swipe(startx, endy, startx, starty, 3000);
  Thread.sleep(2000);
 }

如果您面临更多障碍,请告诉我。