Login

Lost your password?
Don't have an account? Sign Up

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

https://www.educational.guru

46 comments

  1. Automation Step by Step

    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

    1. 3.V Audio & Video

      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.

  2. purani jeans

    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.

  3. Yuvaraj Yuvaraj

    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

  4. ravi singh rajpoot

    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

  5. Mohd Sadique

    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?

  6. Shivam Vashishth

    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.

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*