无法通过 selenium webdriver(Java) 在 Chromedriver 中向下滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34759376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:17:33  来源:igfitidea点击:

Not able to scroll down in Chromedriver by selenium webdriver(Java)

javaseleniumwebdriver

提问by Tanmay

I'm trying to test scrolling functionality by selenium webdriver. Same is working in Firefox but not in chrome driver. Here is basic code I'm using for scrolling.

我正在尝试通过 selenium webdriver 测试滚动功能。同样适用于 Firefox,但不适用于 chrome 驱动程序。这是我用于滚动的基本代码。

Actions a = new Actions(driver);
WebElement el = driver.findElement(By.xpath("//*[@id='dsm-frame']"));
a.moveToElement(el).clickAndHold().moveByOffset(0, 1000000).release().perform();

Is there any specific reason that Action builder does not work in chrome? Kindly advise how it can be worked in Chrome driver.

Action builder 在 chrome 中不起作用有什么具体原因吗?请告知如何在 Chrome 驱动程序中工作。

Thanks

谢谢

回答by Paras

You can use JavaScriptExecutorfor scrolling.

您可以JavaScriptExecutor用于滚动。

Scroll Down

向下滚动

((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scr??ollHeight);");

Scroll Up

向上滑动

((JavascriptExecutor)driver).executeScript("window.scrollTo(0,0);");

回答by Sarah

Javascript Executer version (scrolls to bottom - best for my needs):

Javascript 执行器版本(滚动到底部 - 最适合我的需要):

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight);");

Webdrvier only version:

仅 Webdrvier 版本:

driver.findElement(By.id("INSERT_A_INPUT_BOX")).sendKeys(Keys.PAGE_DOWN);

This version only scrolls down one page. At first it wouldn't work for me because I was trying to focus on a random element not an input element, but it does work when focusing on something you can type into

此版本仅向下滚动一页。起初它对我不起作用,因为我试图专注于随机元素而不是输入元素,但是当专注于您可以输入的内容时它确实有效

回答by Tanmay

So far below are my findings based on options I tired:

到目前为止,以下是我基于我厌倦的选项的发现:

1.Action builder class: Works in Firefox but not in Chrome.Not sure why it did not work in Chrome.

1.动作生成器类:在 Firefox 中有效,但在 Chrome 中无效。不知道为什么它在 Chrome 中不起作用。

2.js.executeScript("window.scrollTo(0, document.body.scrollHeight);");: It neither worked in Firefox and Chrome. I guess this is something not suitable in my case.

2. js.executeScript("window.scrollTo(0, document.body.scrollHeight);");: 它在 Firefox 和 Chrome 中都不起作用。我想这不适合我的情况。

3.js.executeScript("arguments[0].scrollIntoView(true);",element);: It worked in both Firefox and Chrome.

3. js.executeScript("arguments[0].scrollIntoView(true);",element);: 它在 Firefox 和 Chrome 中都有效。

回答by CJDownUnder

I had a similar problem in Selenium (wrapped in Selenide):

我在 Selenium 中遇到了类似的问题(用 Selenide 包裹):

This worked for me to scroll to a link that was off the bottom of the page:

这对我有用,可以滚动到页面底部的链接:

if (isPhantomjs()){
    $(byText(linkType)).scrollTo().click();
} else {
    executeJavaScript("arguments[0].scrollIntoView(true);", $(byText(linkType)));
    $(byText(linkType)).click();
}

回答by Anuraj R

For Scroll down:

对于向下滚动:

WebDriver driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

OR,also

或者,还有

jse.executeScript("scroll(0, 250);");

For Scroll up:

对于向上滚动:

jse.executeScript("window.scrollBy(0,-250)", "");

OR,

或者,

jse.executeScript("scroll(0, -250);");

Hope it will help you

希望它会帮助你

回答by Nitesh

To solve this you have to use this code which is given below 90% chances that u can able to scroll the page

要解决此问题,您必须使用此代码,您可以滚动页面的几率低于 90%

element. driver.findElement(By.xpath("/html/body"));
element.sendKeys(Keys.PAGE_DOWN);

For future query solution visit :

对于未来的查询解决方案访问: