Javascript 向下滚动到带有量角器的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27023768/
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
Scroll down to an element with protractor
提问by andrepm
I have an element on the page that I'm testing that I have to scroll down to be visible. When I execute my test, I get Element is not clickable at point (94, 188)for example.
我正在测试的页面上有一个元素,我必须向下滚动才能看到。例如,当我执行我的测试时,我得到 Element 在点 (94, 188) 处不可点击。
I tried the following:
我尝试了以下方法:
dvr.executeScript('window.scrollTo(0,250);');
But it didn't work. Does anyone know how this works?
但它没有用。有谁知道这是如何工作的?
采纳答案by rajana sekhar
i think this is helpful to you:
我认为这对您有帮助:
dvr.executeScript('window.scrollTo(94,188);').then(function() {
element(by.<<here your button locator>>).click();
})
your webdriver is unable to read that point (1254,21),the reason is your protractor browser unable to cover the full of page what do you want to test, then we give a command that browser is scroll to that point (1254,21), then perform the click operation
您的网络驱动程序无法读取该点(1254,21),原因是您的量角器浏览器无法覆盖整个页面您要测试的内容,然后我们发出命令浏览器滚动到该点(1254,21 ),然后执行点击操作
回答by Rahul Reddy
This seems so late to answer you.. But anyways,
回答你似乎太晚了..但无论如何,
The following code helped me to remove the Element is not clickableerror.
以下代码帮助我删除了Element is not clickable错误。
var elm = element.all(by.css('.your-css-class')).get(9);
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());
elm.click();
Basically this allows you scroll into your view..
基本上这允许您滚动到您的视图中..
回答by Junior Ales
The window.scrollTo(x,x)solution didn't work for me. Specially when testing against ripple emulator.
I managed to make it work using scrollIntoViewmethod.
该window.scrollTo(x,x)解决方案对我不起作用。特别是在针对纹波仿真器进行测试时。我设法使用scrollIntoView方法使其工作。
var scrollToScript = 'document.getElementById("ELEMENT ID").scrollIntoView();';
browser.driver.executeScript(scrollToScript).then(function() {
element(by.id('ELEMENT ID')).click();
expect(...);
});
回答by James Lawson
'use strict';
/**
* Vertically scroll top-left corner of the given element (y-direction) into viewport.
* @param scrollToElement element to be scrolled into visible area
*/
function scrollTo(scrollToElement) {
var wd = browser.driver;
return scrollToElement.getLocation().then(function (loc) {
return wd.executeScript('window.scrollTo(0,arguments[0]);', loc.y);
});
};
Usage:
用法:
scrollTo(element(by.css("div.someclass")));
Disclaimer: this code is from sg-protractor-tools's scroll.js.
Code is licensed under the MIT license.
免责声明:此代码来自sg-protractor-tools的scroll.js.
代码在 MIT 许可下获得许可。
回答by prakash krishnan
I used the link that Bassem suggested and that works. The steps are as follows:
我使用了 Bassem 建议的链接,该链接有效。步骤如下:
We will have to install sg-protractor-tools with the following command (Use Sudo if you do not have admin accesses):
npm install --save-dev sg-protractor-toolsThe code then would look something like this (I tried this on wikipedia)
var sgpt = require('sg-protractor-tools'); it ('verify that scroll works', function () { browser.get('http://wikipedia.org'); browser.manage().window().setSize(1000, 1000); sgpt.scroll.scrollTo(element(by.linkText('Terms of Use'))); } );
我们必须使用以下命令安装 sg-protractor-tools(如果您没有管理员权限,请使用 Sudo):
npm install --save-dev sg-protractor-tools然后代码看起来像这样(我在维基百科上试过这个)
var sgpt = require('sg-protractor-tools'); it ('verify that scroll works', function () { browser.get('http://wikipedia.org'); browser.manage().window().setSize(1000, 1000); sgpt.scroll.scrollTo(element(by.linkText('Terms of Use'))); } );
回答by Bassem
Try using npm module "sg-protractor-tools", working well for me with firefox and chrome drivers,
尝试使用 npm 模块“sg-protractor-tools”,使用 firefox 和 chrome 驱动程序对我来说效果很好,
It's a clean way to do scroll on page elements, More details here https://www.npmjs.com/package/sg-protractor-tools
这是一种在页面元素上滚动的干净方式,更多详细信息请点击https://www.npmjs.com/package/sg-protractor-tools
回答by bhargava krishna
pass the xpath to this method dynamically it will scroll to the element
将 xpath 动态传递给此方法,它将滚动到元素
scrollToElement: function (element) {
return element.getLocation().then(function (location) {
return browser.executeScript('window.scrollTo(' + location.x + ', ' + location.y + ');');
});
},

