How to scroll into view in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone this is mukesh from learn automation calm today in this video tutorial we are going to focus on the scrolling part so the main feature which selenium does not have till date like a scrolling but yes using javascript we can do this scrolling so the main topic of today is scroll into view or scroll for elements so now if you see I will show you one particular application where you will find the scroll inside an element okay so this will be the agenda for today what is scroll interview program for the scroll into view but before going to the actual scenario say this is one dummy application where you have one scroll bar okay that is for the page label where you can scroll up scroll down but now you can see we have individual scroller as well right for this one and for this like this you have so many scroll bars so now let's say and now if you do not a scroll I am getting only scroll bar position and input box now let's say I need to capture this this particular text interceptor sends and some text I need to capture this text using selenium but by default this text is not visible it is hidden so I need to scroll this till this particular element is not visible and once it is visible then stop and capture this text this is my requirement now to do this task now let's find out first the XPath of this I need to capture this text so this is the XPath I am taking one dummy XPath if you are using some custom ways you can customize this XPath as of now for time saving I'm using this XPath and I published the same article long back on my blog so if you go to my blog and search for this topic called scroll interview you will guess that you will get this post and I have written the code as well so from here you can take the sample application and code as well so now scroll into view is just a concept yeah and we'll discuss about JavaScript executed as well so what is crawled into view it will scroll until that particular element is not visible or in the viewport so it will keep scrolling until that element is not visible and once it is visible it will stop scrolling to do this we have to take the help of Java app executor now the question comes what is JavaScript executor so this is an interview question as well like what is JavaScript executed so it is just an interface in selenium webdriver which will allow you to execute JavaScript okay so whatever JavaScript you want to run in selenium you can run the JavaScript using this interface so you have to create reference for this end whatever script you want to trigger you can trigger so in this example we need a small JavaScript that is simple argument 0 dot scroll into view true and pass the element this is just one line of code that you need to add let me repeat again argument 0 dot scroll into you true until that element is not into view keep scrolling so how you will write the code for this I already have the code so this copy paste this code I will show you this is just a dummy code and everything is repeated if so better to do this copy/paste go to eclipse create a new package I will say scroll demo we'll create a separate class called scroll for element in I will create a small test public void scrolling and I will surround this test with test in G test put mouse over here and distant and whatever code you have copied you just paste here so what exactly it is doing and you can see there's some importance missings who put mouse over here import webdriver put mouse over and import Firefox driver import JavaScript executor from selenium import this web element import this by so what exactly does it simply starts a Firefox browser it will maximize it will open the application which I just opened in this Firefox right now it will start creating reference for JavaScript executors now without JavaScript executors we cannot execute any JavaScript to execute this JavaScript we need object of JavaScript executed so now we have type casted our webdriver reference into JavaScript executed so it will give me reference of JavaScript executed this concept in Java is known as down casting okay so generally this complete feature known as casting up casting down casting so we are down casting webdriver object to JavaScript executed so you will get one reference called je using this je object we are calling one method called executor script and just write this script now we need to specify the element which element you are looking so I am looking for this element so we took this XPath right so it says find this element and scroll until this element is not coming into view once it is done say get X element is your scrolling element God get text will capture the text from that element and it will print it's just a one line of code this is that's all and you need to create this object to execute this javascript with the help of one line of code it will automatically scroll until your element is not visible so let's run this code and check how it works so this is scrolling for an element now if you need a scroll for this particular page level same approach only one JavaScript we need to change so right now the method which we used is a scroll into view for scroll page level we have a separate method called scroll I will show you in the next video so this test started and you will see this text is not still present now it will scroll and yeah and you can see now this particular element is into view and if you see here it will capture the text as well and if you do not write this code it will show you element not visible exception not no such element exception and so on so if that particular element is not into view then you have to use this scroll into view and four Paisley will scrolling I will show you in the next video how to scroll up and down in selenium-webdriver because you just try this code from your end and if you want one more example let's use my blog if you go to my blog learn half an automation com let's say you want to capture this text called drop me a line if we need to capture this drop me a line it will keep doing this Auto scroll for you because Shalini of having this auto scroll plus draw a scroll into view will automatically scroll until this particular text or this button is not visible so just try this using this approach and let me know if you find any difficulty so if you find this video useful then please share with your friends and subscribe to my channel if you have any comment feel free to drop me an email thanks so much you
Info
Channel: Mukesh otwani
Views: 72,090
Rating: undefined out of 5
Keywords: Selenium, Webdriver, Selenium Webdriver, Automation, Scroll
Id: l4U76CIntPw
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Sat Mar 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.