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.
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.
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?
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.
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.
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?
Share Improve this question edited Jun 27, 2019 at 23:40 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Aug 11, 2015 at 17:08 Jagadeesh MJagadeesh M 511 gold badge1 silver badge2 bronze badges 2- Try selecting the window before trying to scroll in it, selenium.selectWindow("id=theWindowID"); – Stiffo Commented Aug 12, 2015 at 7:51
- Try to use the "Focus" keyword! It help me! – Laura Commented Nov 30, 2016 at 14:37
10 Answers
Reset to default 4Your 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 Text keyword
*** 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.
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.
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.
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);
The above code will scroll the window and get the element specified in /xpathlocation to the view port.
Execute JavaScript window.scrollTo(1,500) works for me with Python2 ; you have just to put x <> 0 for exemple '1' otherwise it wont' scroll
You can use this as a keyword:
Execute Javascript $(document).scrollTop(${x})
Where,
x is the number in milliseconds to scroll down.
Have you tried in Selenium webdriver in the IPython console directly?
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)')
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 document.querySelector('<div.css>').scrollTop = 200;
You can just change the '200' and increase it as you want to scroll down more.
i tried by pressing tab for number of times till I landed to my element..!
FOR ${i} IN RANGE ${Range}
Press Keys NONE TAB
END
i just ended up by pressing TAB key by required number of times, choose an element and press TAB key
Click Element ${Element}
FOR ${i} IN RANGE ${Range}// you can even provide number
Press Keys NONE TAB
END
I have tried the following it worked for me
Fill in the required info
input text id:userName Joe Biden
input text id:userEmail [email protected]
input text id:currentAddress 211 Madison Ave NY
execute javascript window.scrollTo(0,200)
scroll element into view id:submit
you will need to select the window before scrolling
You can use below.
Execute JavaScript window.scrollTo(0,document.body.scrollHeight)