Selenium 4 Beginner Tutorial 1 | Introduction, Setup & Browser Actions | Step by Step
All FREE courses –
Selenium Quiz –
00:00 Introduction
01:35 Selenium 4
03:48 Getting Started – Project Setup
09:31 1st Test
19:30 WebDriverManager
24:33 Browser Actions
25:25 How to open a web page
26:23 Get current URL
27:00 Get Title
27:53 Forward | Back | Refresh
29:44 Switching windows
32:12 Open new Window
33:26 Open new Tab
36:58 Close Browser
38:00 Frames
44:20 Get & Set Window size
48:49 Get & Set Window position
51:44 Maximize | Minimize | Full Screen
53:06 Screenshot
59:04 JavaScript Executor
01:03:58 Conclusion
Selenium 4
————–
New and improved version of Selenium
Se webdriver can directly communicate with browser using W3C protocol
(without use of json wire protocol as earlier)
New functions added
Multiple windows & tabs management
Relative locators
New Documentation
Getting Started
——————-
Install Java
Setup Eclipse
Create a new maven project
Add maven dependencies for Selenium 4
Download browser driver & add in a folder in project
(Can also keep at any location on your system and add the location in path env var)
Optional
Add TestNG plugin
Add TestNG dependency
1st Test
———
Step 1 – Create a class & main method
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
//timeout driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2)); driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
//close
driver.close();
}
Step 2 – Run code
Using WebDriver Manager
Step 1 – Add maven dependency for webdriver manager –
Step 2 – Add code
WebDriverManager.chromedriver().setup();
Step 3 – To use specific ver of browser
WebDriverManager.chromedriver().driverVersion("92.0").setup();
Browser Actions
———————-
1. Open a web page
driver.get(" ");
driver.navigate().to(" ");
2. Get current url
driver.getCurrentUrl();
3. Get title
driver.getTitle();
4. Forward | Back | Refresh
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
5. Switching windows
String originalWindow = driver.getWindowHandle();
driver.switchTo().window(originalWindow);
6. Open new window and switch to the window
driver.switchTo().newWindow(WindowType.WINDOW);
7. Open new tab and switch to the tab
driver.switchTo().newWindow(WindowType.TAB);
8. Closing browser
driver.close();
driver.quit();
9. Frames
Locate and Switch
WebElement iframe = driver.findElement(By.cssSelector(".rightContainer>frame"));
driver.switchTo().frame(iframe);
Using id or name
driver.switchTo().frame("classFrame");
Using index
driver.switchTo().frame(1);
Return to top level window
driver.switchTo().defaultContent();
10. Window management – Size
Get width & height
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();
Store dimensions & query later
Dimension size = driver.manage().window().getSize();
int width1 = size.getWidth();
int height1 = size.getHeight();
Set window size
driver.manage().window().setSize(new Dimension(800, 600));
10. Window management – Position
Access x and y dimensions individually
int x = driver.manage().window().getPosition().getX();
int y = driver.manage().window().getPosition().getY();
Store dimensions & query later
Point position = driver.manage().window().getPosition();
int x1 = position.getX();
int y1 = position.getY();
Move the window to the top left of the primary monitor
driver.manage().window().setPosition(new Point(0, 0));
10. Window management
// maximize window
driver.manage().window().maximize();
// minimize window
driver.manage().window().minimize();
// fullscreen
driver.manage().window().fullscreen();
11. Screenshots
Take screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./image.png"));
Take element screenshot
WebElement element = driver.findElement(By.cssSelector(".lnXdpd"));
File scrFile1 = element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile1, new File("./image1.png"));
12. JavaScript
Create JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
Get return value from script
WebElement button =driver.findElement(By.name("btnI"));
String text = (String) js.executeScript("return arguments[0].innerText", button);
JavaScript to click element
js.executeScript("arguments[0].click();", button);
Execute JS directly
js.executeScript("console.log('hello world')");
#Selenium4Tutorials
Stories by Raghav –
SHARE with Everyone
If my work has helped you, consider helping any animal near you, in any way you can.
Never Stop Learning
Raghav
Very helpful tutorial. Keep up the good work. ✨??
Thank you so much ?
I can not express my gratitude for you..i am totally beginner but with help of your youtube video i learnt lot today. Thanks a lot
Glad to hear that
What’s New in Selenium 4
* Selenium is now W3C compliant
* Relative Locators
* Better Window/Tab Management
* Improved Selenium Grid
* Upgraded Selenium IDE
* New APIs for CDP (Chrome DevTools Protocol)
* Deprecation of Desired Capabilities
* Modifications in the Actions Class
Great video! I watched all your new Selenium 4 videos, as well as your older videos since learning from them 2 years back. I forgot if your new videos mentioned this so I would like to share this info if anyone else ran into my problem. Upgrading from Selenium alpha-4.0 to the latest 4.0 (as of 11/23/2021) I had to also download the latest geckodriver (for FireFox). Otherwise nothing would work. Hope this helps anyone having upgrade issues.
Hello sir, can you please suggest some tutorial for java based robot framework. Pleas
You rock, thanks so much! this was very helpful and well organized, you’re a great teacher!
You are so welcome! K K
There were a lot of changes in the Actions class – Interaction API, hope you will cover them in the 2nd part ❤️
I will plan all this Prasanth
Hi Raghav Sir, I am a new tester and want to learn selenium for automation testing using Java . So is the selenium 4 playlist enough or are they any pre requisite I need to watch on the channel before I start watching selenium 4 playlist . Will wait for the reply thank you.
Hi, this playlist will be good for you, can also check my selenium java framework here https://automationstepbystep.com/
Hi Sir, your video explained well. have one doubt. if username/password -the class name is the same and id is dynamic because each time refreshed. how can I write a script for this scenario? @Automation Step by Step
Hi Yuvaraj, usually the id is unique but in your case if class name is consistent and can be used to locate the element, you can use that
Hello Raghav, Do you have any videos on how to extend classes and run in a testng.xml file?
If you have then could you please share with me?
Thanks
Yes, Suyen, can check here – https://www.youtube.com/playlist?list=PLhW3qG5bs-L8oRay6qeS70vJYZ3SBQnFa
Hi raghav,
I have question regarding maven dependency part in pom.xml
Once I add dependencies and save pom.xml file I couldn’t find the maven dependency folder in the left side .
Hi Lakshmi, hope you have saved your project, can switch to package view
Thank you so much for this session, please carry on.
Thank you, I will Ankit
He never disappoints…. thank you again sir
You are e legend. Making me look good in my job
Most welcome Mzamo
Thanks for the video. Did not see any notable change in script compared selenium 3. Eagerly waiting for the next part.
I will highlight the changes, next part (coming Thu) will have Relative Locators, that is an imp change in Selenium 4
Great video and practice thank you for actuallity code in selenium 4 .
@Automation Step by Step Thank you.
Most welcome Cristian
Hello Raghav.. thanks much for such wonderful n knowledgeable videos. One QQ is there a way to create a short cut of JMeter over desktop MAC . So that user simple click the icon n JMeter open
basically, you will need to copy all the commands that you use to start JMeter in a text file and then convert into a bat. For mac it can be .sh file, Can check this https://codeburst.io/how-to-create-shortcut-commands-in-the-terminal-for-your-mac-9e016e25e4d7
https://www.switchingtomac.com/tutorials/how-to-run-a-terminal-command-using-a-keyboard-shortcut-on-mac/
@Automation Step by Step thanks much Raghav for the response really appreciate.. request you to please elaborate on the same what command to be passed with .bat file
Yes, you can add a .bat file with the commands
Hello Raghav – I have recently subscribed to your channel as i found the Videos around SE very intresting. I do not know why i am not able to see all videos here in this list?
It says 9 videos are hidden. Any help around it?
Welcome aboard Sadique, they are in process and will be published soon. Mostly you will get one video each week until all are published
Hello Raghav,
I need a bit of help, please help.
And always great work with the videos that you’ve made ?
How can I help you?
Hi Raghav, I recently join your channel and found your selenium series is very useful for all who are looking forward in automation or experienced in Automation. Kindly do post videos as sequence so we can impove our skill by your helping videoes.
Sure will do Shivam
Wonderful! Can you please tell how can i create default profile, so my qr login informations can store?
Hi Ali, do you mean profile on browser like chrome. you can use chrome options. https://www.edureka.co/community/80815/how-to-open-chrome-default-profile-with-selenium
Really superb video lots thanks to you for explanation sharing Knowledge really awesome
So nice of you
Thank you a lot! I cannot even explain how much you are helping me with my selenium class. My university professor doesn’t even explain this clear. ?
So happy to know this is helping Tasha
Yes cls was amazing