Javascript 无法使用机器人框架向下滚动网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31947891/
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
Unable to scroll down the web page using the Robot Framework
提问by Jagadeesh M
I am using Selenium 2 and Robot Framework to automate our application. I have used the below JavaScript code to scroll down the page but am not able to scroll.
我正在使用 Selenium 2 和 Robot Framework 来自动化我们的应用程序。我使用以下 JavaScript 代码向下滚动页面,但无法滚动。
I want to enter text inside the text box after scrolling down, but I am receiving the exception:
向下滚动后,我想在文本框中输入文本,但收到异常:
Element not visible
元素不可见
The text box is partially visible on the screen by default, if we manually scroll down than its completely visible, But selenium robot framework unable to scroll down.
默认情况下,文本框在屏幕上是部分可见的,如果我们手动向下滚动其完全可见,但 selenium 机器人框架无法向下滚动。
I have tried:
我试过了:
Execute JavaScript window.scrollTo(0,200)
Execute JavaScript window.scrollBy(0,200)
Execute JavaScript window.scrollTo(0, document.body.scrollHeight)
How can I fix this?
我怎样才能解决这个问题?
回答by Pekka
Your scrolling code looks ok. However, I don't think scrolling is your problem. Element visibility is ok even if it is scrolled away from screen. Try this code for example. At least on Chrome page scrolls back up at Input Textkeyword
您的滚动代码看起来不错。但是,我认为滚动不是您的问题。即使元素从屏幕滚动离开,元素可见性也可以。例如,试试这个代码。至少在 Chrome 页面上滚动回输入文本关键字
*** Settings ***
Library Selenium2Library
*** Test Cases ***
Scroll
Open Browser http://www.stackoverflow.com/ Chrome
Execute JavaScript window.scrollTo(0, document.body.scrollHeight)
Input Text //*[@id="search"]/input robot framework
Sleep 3
Close All Browsers
I think you may have an incorrect locator for your edit box.
我认为您的编辑框的定位器可能不正确。
回答by Tibin Geo k k
I fixed the issue with Execute JavaScript ${element}.scrollby(0,200)
. It will not work in every case. If the element is not in a viewport it will not scroll.
我用Execute JavaScript ${element}.scrollby(0,200)
. 它不会在所有情况下都有效。如果元素不在视口中,它将不会滚动。
There's an efficient way to scroll the page to get the element to a view port.
有一种有效的方法可以滚动页面以将元素移至视口。
If we are using Execute JavaScript Window.scroll(x,y)
, we need to specify a horizontal and vertical position, x,y, which is difficult to find and may not be accurate.
如果使用Execute JavaScript Window.scroll(x,y)
,则需要指定水平和垂直位置 x,y,这很难找到并且可能不准确。
Instead we can use the following line of code,
Execute JavaScript window.document.evaluate("//xpathlocation", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollIntoView(true);
相反,我们可以使用以下代码行,
Execute JavaScript window.document.evaluate("//xpathlocation", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollIntoView(true);
The above code will scroll the window and get the element specified in /xpathlocation to the view port.
上面的代码将滚动窗口并将 /xpathlocation 中指定的元素获取到视口。
回答by Derrick Lloyd Pua
If there is another element inside your page with scroll bar which you want to scroll into, you can use this Execute javascript command:
如果您的页面中还有另一个带有滚动条的元素要滚动到其中,则可以使用此 Execute javascript 命令:
Execute Javascript document.querySelector('<div.css>').scrollTop = 200;
You can just change the '200' and increase it as you want to scroll down more.
您可以更改“200”并在想要向下滚动时增加它。
回答by ofarooq
You can use this as a keyword:
您可以将其用作关键字:
Execute Javascript $(document).scrollTop(${x})
Where,
在哪里,
xis the number in milliseconds to scroll down.
x是向下滚动的毫秒数。
回答by feiyuw
Have you tried in Selenium webdriver in the IPython console directly?
您是否直接在 IPython 控制台中的 Selenium webdriver 中尝试过?
I have tried as in the following, and it is able to scroll down.
我试过如下,它可以向下滚动。
from selenium import webdriver
firefox = webdriver.Firefox()
firefox.get('http://twitter.com')
firefox.execute_script('window.scrollTo(0,200)')