问题
I have a use case wherein there's a div element on the webpage, it appears like a popup dialog as soon as you click a link (its not an actual popup, its something like dialog boxes which opens in Facebook when you click a link to check reactions on your posts etc.)
I'm using Selenium WebDriver with Java to automate tests for this application, my use case involves me to scroll to the bottom of the dialog box where there is a link to show more items, when user clicks show more, it populates another 10 items in the list and so on until there are no other items left for the user to visit.
So basically I have to scroll down on that particular div element till I keep seeing Show More link and when driver is not able to find show more link it should stop.
Note - I can't just scroll to bottom of the page using javascript window.scrollTo() as it will scroll down through whole webpage, however I just want to scroll to bottom of that division element only.
If anybody have any idea on how to achieve this please let me know.
Thanks for the help in advance !
回答1:
Another way of scrolling to the bottom inside (of) any scroll-able element:
public void scrollToBottomInsideDiv(WebElement scrollArea) {
JavascriptExecutor js = ((JavascriptExecutor) webdriver);
js.executeScript("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrollArea);
}
回答2:
I have had this kind of issue and I resolved this using a similar question asked on SQA SO. Here is the post.
Please see the last answer by Sagar007, which uses a separate function scroll_Page() to achieve this.
Code taken directly from his answer
Some HTML page have internal (custom) scroll bar. We have to handle with little bit different way. Javascript is not working here.
Solution :
WebElement scrollArea =
driver.findElement(By.cssSelector("div.slimScrollBar"));
I added a new step, where I clicked on the div element , using click() method and then proceeded to next step.
Create method scroll_Page as given below. Call this method as
scroll_Page(scrollArea ,100);
Where scrollArea is your dragged(scroll) element and 100 is scroll points.
public static boolean scroll_Page(WebElement webelement, int scrollPoints)
{
try
{
Actions dragger = new Actions(driver);
// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 10;
for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
{
dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
}
Thread.sleep(500);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
回答3:
Use scrollIntoView
scrollIntoView vs moveToElement scrollIntoView vs moveToElement
Pass the more items link element as an argument.
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
You can also use moveToElement
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
回答4:
WebElement ele= driver.findElement(By.id("id_of_element"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", ele);
or
WebElement ele= driver.findElement(By.id("id_of_element"));
Actions builder = new Actions(driver);
builder.moveToElement(ele).click();
Action build=builder.build();
build.perform();
来源:https://stackoverflow.com/questions/44905745/how-to-scroll-to-bottom-of-div-element-selenium-webdriver