简体   繁体   English

如何滚动垂直滚动条(而不是整个页面)?

[英]How to scroll a vertical scrollbar (not the whole page)?

I'm currently trying to scroll a vertical scroll-bar without scrolling the whole page, I want it to implement javascript. 我目前正在尝试滚动垂直滚动条而不滚动整个页面,我希望它实现javascript。

here is the code I'm using with: 这是我使用的代码:

IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.w3schools.com/html/default.asp");
Actions actions = new Actions(driver);
actions.MoveToElement(driver.FindElement(By.Id("leftmenuinnerinner"))).MoveToElement(driver.FindElement(By.Id("leftmenuinner"))).Build().Perform();
for (int i =0;i <= 1000; i++)
{
    ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollBy(0,10)");
    System.Threading.Thread.Sleep(10);
}  

with this code the whole page starts scrolling and not the scroll bar. 使用此代码,整个页面开始滚动而不是滚动条。 so any ideas on how to achieve that? 那么关于如何实现这一目标的任何想法?

The below one worked for me.The element can be a element until which you want to scroll. 下面的一个对我有用。element可以是您要滚动的元素。 WebElement element = driver.findElement(By.id("yourelement")); JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("arguments[0].scrollIntoView();", element);

or you can try the below one. 或者您可以尝试以下一种。

findElement(By.xpath("yourelement").sendKeys(Keys.SPACE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM