Selenium 4 Beginner Tutorial 1 | Introduction, Setup & Browser Actions | Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session on selenium 4. i am raghav and in this session i am going to go very basic step by step so do not worry if you are a complete beginner and if you want after this session you can also take the selenium quiz on my website automation step by step dot com and with that let's get started and i will start from scratch from very basics and we will learn what is selenium 4 what are the new features we will then set up a project with selenium 4 in this session i am going to use java and i am going to use eclipse ide and then we will write our first test with selenium 4. we will see the browser actions in this part and in the next part i will discuss and show you all the elements locators and selectors so in this session we will see how to open a web page how to get the url or the title from the browser how to do forward back and refresh on the browser how to switch windows how to open new tab or new windows on the browser we will see how to close browser and all the commands we will then see how to handle frames with selenium 4 how to adjust the window size and position how to do max minimize and full screen with selenium 4 we will see how to get the screenshots and then finally we will see how to execute javascript on the browser using selenium 4. so this is going to be very easy and very interesting so let's get started and you can follow along with me and here selenium 4 is the new and improved version of selenium and it is the latest release of selenium with some really good features and improvements one of the very prominent feature or improvement in selenium 4 is that now with selenium webdriver we can directly communicate or interact with the browser and we do not have to use the json wire protocol as earlier now just in case you want to understand how selenium used to interact with the browser you can go on my website that is automation step by step dot com and here if you scroll down you will find uh playlist for school of basics here so you can check here and this will take you to my youtube playlist called school of basics and here you can see this video how selenium works so this is the video and here i have explained uh completely from scratch how selenium interacts with the browser so earlier we used to have a lot of apis in between and json wire protocol which is now no longer there and now it follows the world wide web consortium that is w3c protocol and it is much faster and easier and then we have a lot of new functions added in selenium 4 that we will see in this demo we have multiple windows and tab management functions and now we have relative locators so in selenium 4 now we can find the elements using neighbors for example we can use functions like above below left of write off and all that and that we will see in the part 2 of this series so now let us start we also have a new documentation of selenium which is not a part of selenium 4 but in general selenium has a new website and improved documentation and let me show you if you go to the selenium official website which is now selenium.dev you can see all this improved website and here we have the documentation you will see a very clean documentation here so this is a new documentation for selenium and with that let's get started and let us first see what are the steps we have to do so we should be having java on our system and for the ide i'm using eclipse so you can go on your system and i am on my windows system as of now i will open command prompt and to check java version i will say java space hyphen version and here i am getting the java version so that means java is installed on my system it is a older version of java but that's fine it will work if you have a newer version that is also fine now just in case you do not have java you do not get a output of this command you can go on my website again automation step by step dot com and here under the programming section you will see a java playlist here you will find videos on how to install and setup java on windows or mac operating system and for eclipse you can see you can go here id and editors and you can see eclipse here so just in case you want to have eclipse you want to download and set up eclipse you can get it from here so this is kind of a prerequisite for us you should have java and eclipse or any ide that you can work on with java and after this i will go to the third step that is we will now create a new maven project on our ide so i'm using eclipse let me exit from my command prompt and i will open my eclipse here it is and i will create a new maven project so i will go to file new and i will go to maven i cannot find maven here so i will go to others and start typing maven i have got this maven project here select and next and i will say create a simple project skip archetype selection and here i will say next and here i have to give a group id i will say com dot selenium for you can give anything here dot o rg and here artifact id i will say selenium for project you can give anything here and i will say finish and it is it has come here so we have got our selenium 4 project created this is a maven project if you expand this we should be having a palm dot xml file here where we can add our dependencies so we have done this step we have created a maven project now we will be adding the dependencies for selenium 4 and for that go to your pom.xml file and here i will first create a section for dependencies so i will say dependencies i am also getting this auto suggestion you can press ctrl spacebar on your keyboard if you do not get it and yes a start tag and a and tag for dependencies and between this we will add all our dependencies so for selenium 4 let me go to maven repository or maven central search for maven central and here is the maven repository and i will search for selenium java now i will make more tutorials of selenium with javascript and other languages for now let us focus on java so here is selenium java and here we have got the latest version and it is a very recent version just released few days ago so this is the latest stable version of selenium 4 just copy it from here and add to your palm.xml file between the dependencies tag i can also press ctrl a and ctrl i to correct the indentation and as soon as you will save your project it will download all the maven dependencies for selenium and you can see you will have a maven dependencies folder here and here if you scroll down you can see it has downloaded all the selenium dependencies so this is one of the major advantage of using maven all this will be taken care by me when all the dependencies management and that's it we have added the dependency for selenium 4. now we also need to download the browser drivers so that we can run our test on a browser i will show you a way how you can skip this and how you can use webdriver manager but for now let me skip this step and i will show you what happens if you do not add a browser driver so then just in case you want to convert and work with test ng you want to have this project as a test ng testing framework you can add a test ng plugin and then also a test ng dependency so the test ng plugin will be added in eclipse and test ng dependencies in your form.xml but for now i'm not doing that i'm having this session only to show you the different features and functionalities of selenium 4 but just in case you want to add test ng you can do that i in fact have a complete playlist for selenium java framework on my website here or youtube channel as well you can go from here selenium java framework and this will have all the videos where you can see how to add test ng so this is a complete selenium java framework and you can see all the test ng videos will be here so if i just search for test ng you can see here how to uh install test ng what is test ng how to use reports with test ng all this is here so for now i will not use test ng and now we are ready to create our first test so here step number one is i will create a new class and create a main method in the class so here i will just close this i will go to my src test java folder and i will do a right click i will create a new package first i will call this as test you can name it anything inside the package i will again do a right click and create a new class i will call this as first test and you can check this to create a main method public static void main or you can also create it separately so i will just say finish and my class is created i hope the font is fine i can press ctrl and plus on my keyboard to increase the font i hope this is fine for you so now i will create a main method inside the class i can say main and press control space bar on my keyboard and i get this auto suggestion so this is the main method and if you are using test ng then you can always give the test ng annotations but now i will keep all my code in the main method for this demo and now i can now create my code and do not worry about this i will show you what to do so here first thing is i will say web driver driver equals new chrome driver so if you're using a chrome browser you will say chrome driver if you press control space bar it will give you auto suggestion this is what i'm using and here there is a red line under web driver if you hover over this it will show you suggestions so you should be getting this imported from our g dot selenium so og dot open q dot selenium let me again go here and i will import this from org.openq dot selenium and you can see the import statements here now also you can press ctrl shift and o key on your keyboard to manage all the imports and i should also be having a semicolon at the end because this is java so this is how we create a webdriver object and using the webdriver object now i can navigate to a website i can say driver.2 and i can say driver dot get so driver.get is the command i can also say driver.navigate.2 and let us say i want to go to https colon forward slash forward slash google.com so now i will try to run this i will do a right click and run as java application and let us see what happens there is a error if i go to the console you can see it says the path to the driver executable is not set and you should be setting it and there is also a link that you can go and check so that is because we have not used the browser drivers and for that we will have to say system dot set property and here we have to say so here we will use this command webdriver.chrome.driver for chrome browser and then give the location of chromedriver.exe so i will say webdriver dot chrome.driver and here to give the location we first have to download the chromedriver.exe so here i will go to my browser and say chrome driver download now in a moment i will also show you a way to skip this manual step but for now let me go here so this is the website and you can see all the downloads now here you have to be a little careful uh get the version of the chrome driver which is in sync with a version of the browser on your desktop for example this is my chrome browser on my desktop my primary browser and if i go to the help and say about google chrome i can see the version here is version 94 so i should be getting the chrome driver as well which is for version 94 so i will get this one and i will get it for windows which is this one and that's it so once this is here i will unzip it i will extract it and it is here now you can keep it anywhere on your system and then give the location in your code but a good way is you can keep it within your project folder so that once you take your project folder anywhere you can also take your browser driver along with it there is another way that you can keep it at any location and add the path of that location in your path environment variable so that it can be accessed from anywhere but for now let me just create a folder inside my project i will do a right click on my project and say new and create a new folder and say the name is drivers and i will put my chrome driver under this and sometimes if you do it directly on eclipse it may not work so let me just open it sorry i will have to open this folder on my system so let me go to properties and yes from here it will get opened yes so this is my drivers folder and i will manually take my chrome driver and put it in this folder and that's it now i can close all this and on my project now if i do a right click and say refresh i should have the chrome driver here so now i have to give this location here and you can give the complete location that is the absolute path or because this is now in my project folder i can give a relative location i can say dot which which is the location of my project folder that is selenium 4 project and then forward slash go to the drivers folder and then we go to the chromedriver.exe and that's it you should have a semicolon at the end and that's it i will save and i will run again and check so here let me see we have done this so we have set web driver driver equals new chrome driver and here this is all fine okay i think there's a spelling mistake this is chrome driver dot exe save and run again and yes looks like this will now run and it goes to google.com and it has not closed the browser because we do not have any command here so i can add the command here for closing the browser as well so here i will say driver.close and it will now close the browser as well and let me run it again it opens the chrome browser goes to google.com and then closes it and now uh we can also have some weight here so if i go to the selenium official website and here if i go to the documentation and getting started and i will say here how to upgrade to selenium 4 you will see some changes here so these are what was before and what is after selenium 4 what are the code changes for finding elements this is how we used to do before and this is the new statements or commands now for finding multiple elements and then this is how the maven dependencies cradle dependencies and all these in different languages and here you can see how to manage the timeouts and weights this was the code earlier and this is how it looks like now so let me copy this and i will put it here so that i can also test it you can see it says driver.manage.timeouts and then we can set the implicit weight we can set the script timeout we can set the page timeout and duration is coming from java.time so i will import the duration class here and that's it and i can set what is the timeouts i want let us say i want five seconds for implicit wait uh script timeout is two minutes and page load time out also let me keep it 10 seconds which is fine and i will now test it again so this opens google.com and closes the browser so this is fine and we are able to create our first code with selenium 4. we have already done step number two that we have ran our code and this is all working fine so if you have done this as well and you are following along with me so congratulations you have ran your first test with selenium 4 and now we will go and see how we can use webdriver manager to manage all our browser drivers without to manually without we having to manually download and set up the browser drivers so webdriver manager is a library that can manage all the browser drivers for us i had earlier created a video in my selenium java framework as well so you can see this one webdriver manager i have shown here how to use webdriver manager for beginners with examples so you can check that or let us do this now so here step number one is we have to add the dependency for web driver manager you can go to this website bonnie garcia dot dev dot forward slash web driver manager to get all the information but for now i will directly go to the maven repository and search for web driver manager and here i will this is coming from this one make sure you get it from io dot github dot boni garcia and go here this is the latest version i will go here and i will just copy the maven dependency and go to your pom.xml and i will just add the maven dependency here and i will say control a to select everything ctrl i to correct the indentation and save control s and as soon as you will save you will had you will have all the dependencies added so you will see all the web driver manager dependencies will be added here so it is it has come here just in case sometimes uh the save option or the when you press ctrl s does not download all the dependencies you can always go to project and then you can clean and build automatically then it will get all the dependencies added so we have done step number one now step number two is we just have to add this code webdrivermanager.chromedriver.setup so i will say here in my code i will say now i do not need this i do not have to manually download chromedriver.exe and set its path i will say webdriver manager you can also press ctrl spacebar for auto suggestion dot chrome driver dot setup and that's it that's the only thing we need and let us see what happens now i will run this and check so it opens the browser so that means it is working fine it opened the browser goes to google.com and closes it so that means it is working fine if i check the console you can see it is starting the chrome driver which is the latest chrome driver and then it is taking care of everything without we having to manually do that browser management now sometimes it will get the latest version which will work for your system but sometimes if you want to get a specific version even that you can do by adding the function driver version so let's just say suppose i want let's just say suppose i want some particular version let us say i want this version of chrome so i will say here webdrivermanager.chromedriver and then i will say dot driver version and i will give the version here and dot setup that's it and again i will save and run and check so it still opens the browser goes to google.com and closes it and if i now check the console you can see it has got this version of chromedriver so you can get a specific version and the similarly it will work for firefox and other browsers as well just in case you want to try you can go to the website of webdriver manager so if you go to web driver manager and here is the website and you can see here all the details so all the documentation how it is used and all the support here you can check all the details from here so if you go to here this is the documentation the latest one and you can see how it works and then for different browsers chrome firefox edge opera ie driver how to setup and how to use everything is here so this is how we can use webdriver manager and we can skip the manual management of browser drivers now let us go to browser actions and for that let me first create a new class for browser actions so i will go to my test package do a right click new and i will create a class and i'll just name it browser so i will know all my browser related actions are in this class i will again create a main method control space bar for auto completion and now first let me just copy the this code so that i do not have to write it again so this is my initial code and i will say driver dot close at the end and that's it and now here first we will see how to open a page so i can say driver.get or i can say driver.navigate.2 so here i have already used driver.get and if i want i can also say here driver dot navigate dot 2 and then i can give the url here and then there should be a semicolon at the end so let's say i want to go to https and i'll go to my website automation step by step dot com and let us see what happens i will run this uh sorry i will have to first do a right click and say run as java application and let us now see so it goes to google.com and then it goes to automation step by step dot com and closes it so both of these actions or commands we can use for opening a web page the next action is we can get the current url by saying driver dot current url so i can say driver dot get current url and just to show you on the console i will print this out so i will say s5s 0 and i will press ctrl space bar on my keyboard for auto completing the system.out.println statement and this i will put inside the print statement here and that's it so it will get the url uh let me also see what is the next action the next action is get title and again i can say driver dot get title so again i will say here syso control space bar and i will say driver dot get title so it will also print the title and let me now run it and check it opens the browser goes to google.com and then goes to automationstepbystep.com and closes it if i go to the console and see so this is the url and this is the title so this is the title that says never stop learning automation step by step and if you see here on my website this is the url and if you see that title it is never stop learning automation step by step and that's what is printed here so this is working fine and then if you want to do a forward backward or do a refresh of the browser you can use all these navigate dot back navigate dot forward navigate dot refresh so let me show you here i can say driver dot navigate dot back driver dot navigate dot forward and i can also refresh my browser by saying driver dot navigate dot refresh now it will be very fast so just to see what's happening on the screen i will add some weight statements so i will say thread.sleep now this is a hard wait it is not recommended to use this in your actual project but just for now to show you the demo i am using this thread.sleep and as soon as i add here it tells me if i hover over it it add tells me either i have to add a throws declaration or i i have to surround this with try catch so that i can catch the exception for now i will say add throws declaration and i will just say throw interrupted exception in my function heading here and then that's it i will also add this dot sleep here and also just before closing the browser i will save and run and check it opens the browser goes to google.com then automation step by step dot com it goes back goes forward refreshes so it is refreshing now and then finally it will close so all this is working fine so these are the common and very basic actions now the fifth action is we can also switch windows now this is very handy and very useful sometimes in your browser on your application if you click some link or some element it opens a new tab or a new window and if you want to switch to the window or switch back to the original window we can use this switch to window so here i can say i can see here window dot get so i can just say driver dot get window handle driver dot get window handle so every window has got a id which we can get using the window handle get window handle and i can actually store it in a string so i can say string and i can name use any variable let us say original window and then i can say here driver dot switch to window and i can give the variable name here so i can say driver dot switch to window and i will give this variable which has my original window handle so i can use like this and let me say i keep this here just after google.com and then after navigating to all these i am finally switching to the original window so let me now save and run and check what happens it opens the browser goes to google.com automation step by step dot com forward and backward and refresh and yes refreshes and it closes so as of now we do not have a new window but in a moment i will show you how exactly this works but for now just remember whenever you have multiple windows and you want to switch between windows you can always store the window handle and then switch to the window this is a browser action for switching windows and then if you want to open a new window and then switch to that window you can say driver dot switch to new window window type window so i can say driver dot switch to new window and i can say window type is i will say window here and that's it and i will save and i'm just commenting out the driver.close so that we can actually see if the new window is created or opened i will save and run now let us see so it is going to google.com and then all this and now it is refreshing and now yes you can see it has opened the new window so new window is opened here and for now i will close it manually and similarly i can also create a new tab and switch to the new tab so i can say driver dot switch to new window and i will say window type is tab and let us now see i will run this and all this is happening as per our earlier commands forward backward refresh and now you can see it has opened a new tab here so we can see here we have a new window and then a new tab as well so this is a new window and then it went back and opened a new tab as well now here let us try our earlier action that is for switching with windows handle so here let us say driver dot switch to window handle original window i will cut from here and paste it here and let us now see what happens so we had stored the original window after going to google.com so when we went to google.com that is our original window and after doing all this we are switching back to our original window let us see now i will save and run it opens the browser goes to google.com and this is our original window now then automation step by step dot com and then again forward backward and it will open a new window a new tab and then you can see let me just close this let me just close this manually and here i will comment out all this i do not want to go to a new browser or a new application and also i'll just comment all this i will say forward slash star and i want to comment until here star forward slash so all this is now commented we have all this and without switching to the original window let us see what happens i will save and run goes to google.com and you can see it has opened a new window and then it has also opened a new tab now the last moment it was the control was on the new tab but if i use driver dot switch to window original window let us see now i will save and run and let us see what happens now it opens the browser and yes you can see now it has switched back to the original window that is google.com so now we can see it is working fine and now i will also uncomment driver dot close so here this is how all these actions are working the seventh action we have already done that is open a new tab and switch to the new tab and then we have als already seen closing browser driver.close and driver.quit now when you say driver.close it will only close the current window or the tab but not the complete session but when we say driver dot quit it will end the complete session of the browser so let me say we have driver driver.quit and for now let me just comment this out i'm only using driver.close and i will run my code let us see and you can see you can see here it has closed the last opened tab but it is still it has still not closed the complete browser session but when i say driver dot quit now this will close the entire browser session so let us see i will run this and you can see everything is now closed so that is why we use driver.close and quit so these are the basic actions and now we will move to frames now currently frames are not very much used in web development so from html5 uh onwards we have very less use of frames but you may have a scenario or you may you may have a situation where you have to deal with frames let me first show you what is a frame so let me show you some example if i go to some website which has frames this web website must be having frames here and yes so this is a website with frames you can see three frames here and one of the ways to find out if it is a frame or not you can do a right click and you will see view frame source reload frame all this and also if you go to right click and inspect you will see in the dom you will see iframes here so if i see here you can see here iframe so if you see this tag iframe then that means it is a frame now earlier whenever uh developers have had to show some ad or some third party content uh frames were used so all the third party content or ads were used to show in frames but it is no longer used now or very much so still if you have anything like this you can use the frame actions or commands in selenium now let us say if i have to click on this alert button i will first go to this website so i will say here let me just comment all this and i will say here driver.navigate.2 and i will use the url which is this and then let us see i have to click on this alerts link this is an alert link here i have to click here so for that i can say driver dot find element by dot i can use link text so do not worry i will have a separate session on all the locators and selectors in part 2 of this video but for now i will just say find element by link text alert and then click on it so dot click let us see what happens i will save and run so it opens the browser first goes to google.com then goes to this website but you can see it is not able to find the element and it is not able to click and if you see the console here it says you can see it says here there is a error and the error is it is not able to find that element so you can see here all this here this is the error no such element so it is not able to find the element and that is because the element is inside a frame and we have not yet switched to the frame so for that we will first need to switch to the frame and for that we have actions in selenium so you can switch to frame you can say driver dot switch to and then give the frame name or the frame id let me show you this i will first have to switch to the frame i will say driver dot switch to dot frame and i can use the index of the frame if you know what is the index of the frame it starts with index 0 and if you know the index of the frame you can use the index of the frame or you can also use the name for example if i do a right click and say inspect i have to check what is the name of this frame or the class so if i go here this is the frame and the name is package frame so you can see here the name of this frame is package frame this is the iframe so i can just say here i'll copy this and i will use this here and then i will click on the link after switching to the frame let us now see i will run and check again so it opens the browser goes to the application and you can see it has clicked on the alert link so it was very fast i can increase the timeout here or the wait time and you can also use so here you can use a css selector as well so here we have used dot write container which is the class name in case you have the class for the frame so you can see here is the class left bottom so you can say uh first find the element by css selector dot left bottom and then iframe like we see in this example and then you can switch to the frame or in this case like i have done you can directly use the id or the name and then you can also use the index position now after doing this if i have to click on any other element which is outside this particular frame so i have used i have switched to this frame now if i have to click anywhere outside or use any other element i will not be able to do that until i switch back to the default content and that is very important so whenever you switch to a frame after doing your actions inside the frame always switch to the top level window by using the command switch to default content so first i will say here driver dot switch to default content and then you can do the rest of the actions so i will run this again and check so it goes to the application clicks on alert link and switches back to default content and then it closes it so this is how we can use frames in selenium 4. now let us see the window management in selenium 4 and first let us look at the size management so you can get the width and height of your browser window by using driver.manage.window.getsize so i can see here let me first comment this out until here and then i will say driver dot so i will say driver dot and we will say manage dot window manage dot window dot get size and then i can get the height and width so i can directly get width like this and i can also get the height so i have to say get width and then similarly i will say driver dot manage dot window dot get size dot get height and if you want you can store it into a variable like i have shown here you can use a integer variable and then can print it now you can do like this or you can directly use a dimension type of variable to get the size and then from that particular variable you can get the height and the width so i can say i can use dimension and i can say i can give any variable name and i can say driver dot manage dot window dot get size and height and width will be stored in this size variable you will have to import it from org dot open qa dot selenium and now using the size i can get height and width so let me print it out syso control space bar for auto completion and i will say size dot get width and i can say size dot get height and i will save and run this and let us see it goes to google.com and if i check the console you can see the width and the height are printed here so this is how you can get the width and height and all these notes will be available in the description or the notes section of this video so if you want to directly copy the code you can get it from there and you can also set the window size by saying driver.manage.window.setsize and then give the dimensions i can say here driver dot manage dot window dot set size and i will say i'll give the dimension and i will say you can set the size here so i can say any size let us say 800 width and 600 height so i can see like this and let me see if anything is missing here so this should be fine let me check here what it says okay uh sorry i added as a string which is not correct it should be like this yeah this is fine and let me also keep the weight or the sleep time to three seconds so that you can see the new size of your browser window and i will run this now and you can see it has changed the size it is 800 by 600 and then it closes it so this is how you can set the window size and then let us see the position window management position so you can also get the position and set the position and here we use the get position function of the web driver and then you can get the accent y position of your window so i can say here driver dot manage dot window dot get position dot get x and you can store it in an integer variable and print it out as well just to check or you can also say driver dot manage dot window dot get position dot get y so this will give you the y coordinate of your window and you can also get it a store it and then query it later by using a point type of variable so you can say point like we had used dimension for size for position i can use point and i will say position or you can use any variable equals driver dot manage dot window dot get position and i will have to import the point from org open q selenium and now i can say i will print out syso control space bar i will say position dot get x and position dot get y so let us see this i will save and run and let us see what is the x and y position of our window and you can see as of now it is 10 10 so in the console you can see it is 10 10 and this is relative to the uh top left corner of the monitor so this is what our position is and we can also set a new position by saying set position so i can say here driver dot manage dot window dot set position and i will have to give a new point type of data here and then i can give the x so let us say x i want to move to 500 and why i move want to move to 100 and that's it and let us now see i will save and run and let us see and you can see it has moved the window to this position and then finally closed it so this is how you can set the position and i hope this is getting interesting let us see now we will see how we can maximize minimize and do full screen of our window so it is very simple you can just have to you just have to use the commands maximize minimize and full screen so i will say driver dot manage dot window dot maximize and then i will say driver dot manage dot window dot minimize and then driver dot manage dot window and i will do a full screen and to see this in action i will have to use some wait time or i have to use some sleep time so let me just put a two seconds sleep between these actions so i will say two seconds here so this is in milliseconds two thousand milliseconds means two seconds and here as well and that's it i will save and run and check so first it was maximized and minimized and now full screen and then finally it will close the browser so this is all working fine so this is how we can do window management in selenium 4. now let us move to screenshots in selenium 4 we can take screenshots this was also available earlier so now if you want to take a screenshot you will have to type cast the driver in with takes screenshot class and then we use the get screenshot as function so i will say driver let me comment until here and i will remove this one so that everything gets commented until here and now i will say takes screenshot driver so this is my driver i will type cast takes screenshot if i press ctrl spacebar it auto completes it and then i will say here dot get screenshot as output type file so here uh okay i forgot the brackets here all right now i will say output type file and that's it and let me also store it in a file variable so i will say file src file you can use any variable here and i will have to import it so let me see i will get it from java dot io so that's it this will take the screenshot now i will have to store it so i will use the file utils and then copy it and i will say so here i will say file details and let me get it from i will say file utils so this is a dot copy file and i have to give the source file which is this one and the destination i can say so i can give any destination here you can give any destination and it's better to keep it inside your project so i will say here dot this takes me to the parent folder of my project and then forward slash and i can just say image one dot png and i will have to first add a throws declaration so i'll just add this i o exception here you can also use a try catch block but let me just keep it as it is and now i will save and run and check if i get a screenshot stored in image1.png file in my project folder so this is done now i will refresh my project folder i will do a right click and refresh and you can see image one is here and it has taken a screenshot of the browser so this is how you can take a screenshot you can also take a screenshot of a particular element so for example uh let us say in this google.com let us say i want to take a screenshot of this particular element let me do a right click and say inspect and let me see if there is any property here so here yes i have the class property so i'll just copy this class and i will say here i will say i'll use the web element and i will say driver dot find element by dot i can use a css selector and because this is a class i can say dot in case of id you can use hash i will say dot and this is the name of the class and i have got a web element stored here so i will also have to import the web element from org open q selenium so this is now stored now i will just have to use element instead of driver so here i will just say i'll copy this same command here and the first thing is i have to change the variable name so instead of src file i will say src file1 and instead of driver i'll just have to say element whatever you have used as the variable name here the same thing i will use here this element here and then we have to use the same file details to copy so i will just copy this statement here and here i will say sources src file1 and here i will say store it image2.png so let us now see i will save and run again so this time it should also take the screenshot of that particular element and this is done if i refresh my project i have got image 2 as well and you can see image 2 it has taken the screenshot of this element so this is how we can take the screenshots so this was interesting let us move to the javascript executor in selenium 4 we can execute javascript so first we have to create a javascript executor interface object so i will say javascript executor i will say here javascript executor and give any variable name equals to and then i will have to typecast my driver to javascript executor so i will say driver and i will say here javascript executor and that's it also let me comment all this until here so this is how we create a javascript executor interface object now using js i can execute my javascript so in case you want to get a return value from a script you can uh for example in this example we are having a button and then from that button we are getting the inner text so i can say js.executescript return arguments dot a text so it will get whatever is the inner text of that button or if you want to uh click on a button you can do that for example let us say here on google.com i want to click on this button using javascript let me first inspect the button and here this button has a name so we have a name property so i will just copy this name and i can first create a web element i will say web element i can give it any name button equals driver dot find element by dot name and this is the name let me copy the name again this is the name btni and then i have to use javascript to click on this button so i will say gs.execute script and i will say argument 0 dot click gs dot execute script and i will say here arguments this is how we can run a javascript and here i will use the name that i have used here button so this is how i can run a javascript let me save and run and check what happens so it goes to google.com and you can see it clicked on that button using javascript and also you can directly run a javascript and you can say write something on the console so i can say gs.execute javascript gs dot execute script and here i will say console dot log and i will say hello world here and that's it so let me just close it here and i will run and to see this i will have to comment out driver dot closing driver dot quit so that i can show you it has written hello world on the console of the browser so i will save and run now and let us see so it clicks on the button and now if i have to check the javascript console i will do right click and go to the dom i will say inspect and i will go to the console and you can see here it has written hello world so this is how you can use javascript let me uncomment driver.close and driver.quit and this is how we can run a javascript on the browsers using selenium so i hope this was very interesting and i will tell you what all we will learn in the next session and just to recap today we learned the selenium 4 introduction and features we saw how to set up the project with selenium 4 how to write the first test and we saw all these browser actions you can take a quiz on my website automation step by step dot com and in the next session we will deal with selenium four elements locators and selectors that will be very interesting i hope this was very useful for you thank you for watching and never stop learning
Info
Channel: Automation Step by Step
Views: 23,065
Rating: undefined out of 5
Keywords: selenium 4 beginner tutorials, selenium tutorial for beginners, selenium interview questions and answers, selenium projects for practice, what's new in selenium 4, how to setup project with selenium, selenium how to take screenshots, selenium javascript executor example, selenium step by step with raghav pal, selenium step by step tutorial for beginners, selenium 4 java project
Id: HjYEjKWSbe8
Channel Id: undefined
Length: 64min 37sec (3877 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.