问题
I want to scroll an android mobile app page from top to bottom.
I have tried with below defined coding for scroll and click for specific web element using text. It works fine.
// method 1 driver.scrollTo("R"); // method 2 driver.ScrollToExact("Top");
- But I need to scroll an full article page from top to bottom, without using above scroll() methods. I have tried with below coding, but scroll action doesn't happens for me.
// scroll to bottom of an page
((JavascriptExecutor) driver) .executeScript("window.scrollTo(0, document.body.scrollHeight)");
- How can I scroll an android app page from top to bottom using appium driver?
回答1:
Here you need to check everytime with a text
in the loop, that appears on the bottom of the page. If the text found than break the loop.
Dimension screenSize = driver.manage().window().getSize();
int startx = screenSize.getWidth() / 2;
int endx = startx;
int starty = (int) (screenSize.getHeight() * 0.70);
int endy = (int) (screenSize.getHeight() * 0.20);
AndroidDriver androidDriver = (AndroidDriver) driver; // WebDriver to AndroidDriver
while(true) {
// code to check with a text which is appears on the bottom of the page
//break;
scroll(driver, startx, starty, endx, endy, 500);
androidDriver.swipe(startX, startY, endX, endY, time);
}
回答2:
You can use in loop:
page.swipe(SwipeElementDirection.UP, 400)
回答3:
Use :
driver.scrollTo("text");
Where text is the name of the button(You can check the text in UIAutomatorviewer)
回答4:
Step 1
public boolean isElementFound(String zoneName, String text, int index) {
try{
//text=text.concat("["+index+"]");
//System.out.println(text);
WebElement webElement = appiumDriver.findElement(By.xpath(text));
System.out.println("isElementFound : true :"+text + "true");
//ReportAppium.getSnapShot(appiumDriver);
}catch(NoSuchElementException e){
System.out.println("isElementFound : false :"+text);
//ReportAppium.getSnapShot(appiumDriver, "isElementFound- "+text + "false");
return false;
}
return true;
}
Step 2
while(isElementFound(element)==false)
{
public void swipe(int startx, int starty, int endx,int endy,int duration)
{
TouchAction touchAction = new TouchAction(appiumDriver);
System.out.println(startx+" "+starty);
System.out.println("Entering swipe");
System.out.println("Swipe from "+startx +" " +starty +"to" +endx +" " +endy );
touchAction.press(startx, starty).waitAction(duration).moveTo(endx,endy).release().perform();
}
}
回答5:
Use
driver.scrollTo("Text");
Note: Text content you can find from node detail screen of UI Automator Viewer Tool
来源:https://stackoverflow.com/questions/30867765/how-can-i-scroll-an-android-app-page-from-top-to-bottom-using-appium-driver