0% found this document useful (0 votes)
13 views7 pages

Selenium WebDriver Notes1

The document provides an overview of Selenium WebDriver architecture, detailing the interaction between Java code, the WebDriver API, and browser drivers. It covers various WebElement methods for handling input fields, buttons, radio buttons, checkboxes, dropdowns, alerts, frames, and multiple windows or tabs, along with code examples for each. Additionally, it discusses implicit and fluent waits for managing element visibility and interaction timing in automated tests.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Selenium WebDriver Notes1

The document provides an overview of Selenium WebDriver architecture, detailing the interaction between Java code, the WebDriver API, and browser drivers. It covers various WebElement methods for handling input fields, buttons, radio buttons, checkboxes, dropdowns, alerts, frames, and multiple windows or tabs, along with code examples for each. Additionally, it discusses implicit and fluent waits for managing element visibility and interaction timing in automated tests.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Web Driver Architecture

[Link]("[Link]
|
Your Java Code

Selenium WebDriver API

Browser Driver (ChromeDriver)

Real Browser (Chrome)

• Java sends command → WebDriver


• WebDriver sends instruction → ChromeDriver
• ChromeDriver talks → Chrome browser
• Browser opens the URL
Result goes back to your cod

===============================================
[Link] Driver List

Google Chrome-ChromeDriver,Mozilla Firefox-GeckoDriver,Microsoft Edge-EdgeDriver,Apple


Safari-SafariDriver

=================================================
[Link] methods

WebDriver controls browser


WebElement controls elements

Action methods
Method Purpose
sendKeys() Enter text
click() Click button/link
clear() Clear existing text
submit() Submit form
Validation methods

isDisplayed() Element visible or not


isEnabled() Can interact or disabled
isSelected() Checkbox/radio selected
Get methods

getText() Get visible text


getAttribute() Get attribute value
getCssValue() Get CSS property
getTagName() HTML tag name

====================================================
[Link] input fields & buttons

code:-
WebElement username = [Link]([Link]("username"));
WebElement password = [Link]([Link]("password"));
WebElement loginBtn = [Link]([Link]("//button[@type=’submit’]"));

// Fetch attribute value


[Link]([Link]("name"));;
// Fetch tag name
[Link]([Link]());
// getText returns empty for input fields
[Link]([Link]());
// Fetch CSS value
[Link]([Link]("color"));
// Check visibility & enablement
[Link]([Link]());
[Link]([Link]());
[Link]("Admin");
//it wont work becasue that feild is handle by JavaScript
[Link]();
[Link]();
[Link]([Link]+"a");
[Link]([Link]);
[Link]([Link]("value"));
[Link]("Admin");
[Link]("admin123");
[Link]();

============================================
[Link] buttons & checkboxes

WebElement radioButton = [Link](By.


xpath("//div[@class=’oxd-radio-wrapper’]" +
"/parent::div/parent::*/parent::*/child::div/following-sibling::*"));
if (![Link]()) {
WebDriverWait waitRadio = new WebDriverWait(driver, [Link](10));
[Link]([Link](radioButton));
[Link]();
[Link](1000);
}

WebElement checkbox = [Link]([Link]("(//span[contains(@class,’oxd-checkbox-’)])[2]"));


if (![Link]()) {
[Link]();
[Link](1000);
}

=============================================
[Link] (Select class)
• Selenium provides Select class
• Only works with <select> HTML tag

Method Purpose
selectByVisibleText("Admin") Select option by text
selectByValue("1") Select by value attribute
selectByIndex(0) Select first option (0-based index)
getOptions() List all options in dropdown
getFirstSelectedOption() Get currently selected option
deselectAll() Deselect all (only for multi-select)
Code:-
[Link] --Done

// Click the dropdown


WebElement clickDropDown = [Link](By.
xpath("(//div[@class=’ui dropdown selection’])[1]"));
[Link]();
// Wait for the individual options to be visible
[Link]([Link](By
.xpath("//div[@class=’menu transition visible’]//div[@class=’item’]")));
//div[@class=’menu transition visible’]//div[@class=’item’]
// Get all items inside dropdown
List<WebElement> options = [Link](By.
xpath("//div[@class=’menu transition visible’]//div[@class=’item’]"));
String gender = "male";
for (WebElement option : options) {
if ([Link]([Link]().trim())) {
[Link]();
break;
}
[Link](2000);
}
[Link](2000);
[Link]();
}

===============================================
[Link] (simple, confirm, prompt)

1 1 Simple Alert
Only OK button
Example: “Saved successfully”

2 2 Confirmation Alert
OK + Cancel
Example: “Are you sure you want to delete?”

3 3 Prompt Alert
Text box + OK + Cancel
Example: “Enter your name”

Alert Methods
Method Purpose
accept() Click OK
dismiss() Click Cancel
getText() Read alert message
sendKeys() Enter text (prompt alert)

Code:-

// //Simple Alert
// WebElement ele=[Link](By.
// xpath("//label[text()=’Click Button to see alert’]//following-sibling::*"));
// [Link]();
// [Link](5000);
// [Link]().alert().accept();
// [Link](3000);
////Confirmation Alert
// [Link]([Link]("(//label[text()=’Click Button to see alert’]//following::*)[7]")).click();
// [Link]().alert().accept();
// [Link](3000);
// //Prompt alert
// [Link]([Link]("(//label[text()=’Click Button to see alert’]//following::*)[11]")).click();
// WebDriverWait wait = new WebDriverWait(driver, [Link](5));
// Alert alert = [Link]([Link]());
// [Link]("muthu HI");
// String promptMes = [Link](); // read FIRST
// [Link](promptMes);
// [Link]();
//Wait alert
[Link](By
.xpath("//label[text()=’On button click, alert will appear after 5
seconds’]/following-sibling::*")).click();
WebDriverWait wait = new WebDriverWait(driver, [Link](5));
Alert alert = [Link]([Link]());
[Link](2000);
[Link]();

=============================================
[Link] & iFrames

Web Page (Main HTML)


└── Parent Frame
├── Child Frame 1
│ └── Element A (Button / Input)

└── Child Frame 2
└── Element B (Dropdown / Text)

Methods:-
1..parentFrame();
[Link]();

Code:-

[Link]().frame(1); // switch to frame2 under main page


[Link]().frame(2); // switch to frame3 inside frame2
[Link]().parentFrame(); // back to frame2
[Link]().frame(1); // now switches to **child frame index 1 of frame2** again

code:-
WebElement paragraph = [Link]([Link]("//p[contains(text(),’Sample Iframe’)]"));
[Link]([Link]());

// Step 1: Switch to first iframe


[Link]().frame(0);
WebElement iframeText = [Link]([Link]("//h1[contains(text(),’Selenium - Automation
Practice Form’)]"));
[Link]([Link]());
// Step 2: Back to main content
[Link]().defaultContent();
[Link](1000);

// Step 3: Switch to second iframe safely


// WebElement secondFrame = [Link]([Link]("//h2[text()=’Iframe
2’]/following-sibling::iframe"));
// [Link]().frame(secondFrame);

// Step 4: Wait until link is clickable


WebElement linkText = [Link]([Link]([Link]("Selenium
Tutorial")));

// Step 5: Scroll into view


((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", linkText);
[Link](500);

// Step 6: Click safely


try {
[Link]();
} catch (ElementClickInterceptedException e) {
// fallback: click via JS if intercepted
((JavascriptExecutor) driver).executeScript("arguments[0].click();", linkText);
}

[Link]();
}
}
================================================

[Link] windows / tabs

Selenium always stays on the parent window unless we switch explicitly.

Method Use
getWindowHandle() Get parent window ID
getWindowHandles() Get all window IDs
switchTo().window(id) Switch to window
close() Close current window
quit() Close all windows

Code:-
String parentWindow = [Link]();
String title = [Link]();
[Link](title);
[Link]([Link]("//button[text()=’New Tab’]")).click();
Set<String> secondClick = [Link]();
String childWindow1 = null;
for (String window_ : secondClick) {
if (!window_.equalsIgnoreCase(parentWindow)) {
childWindow1 = window_;
[Link]().window(window_);
break;
}
}
WebDriverWait wait = new WebDriverWait(driver, [Link](10));
[Link]([Link]([Link]("//a[@class=’external-link’]")));
[Link]([Link]("//a[@class=’external-link’]")).click();
String text = [Link]([Link]("//div[@class=’container’]/child::div")).getText();
[Link](text);
Set<String> thiredClick = [Link]();
String childWindow2 = null;
for (String tClick : thiredClick) {
if (![Link](parentWindow) && ![Link](childWindow1)) {
childWindow2 = tClick;
[Link]().window(childWindow2);
[Link]([Link]());
break;

}
[Link]([Link](By.
xpath("(//div[@class=’center-aligned tutorial-menu’]/form/child::*)[4]")));
[Link]([Link]("(//div[@class=’center-aligned tutorial-menu’]/form/child::*)[4]")).click();
[Link]();

===========================

[Link] wait

Implicit wait is global and not recommended in complex frameworks.

How it works?
• Applies to ALL elements
• Polls DOM until element found or time ends
Drawbacks
• Applies globally
• Cannot wait for specific condition
Slows test if overused

Code:-

WebDriverWait wait = new WebDriverWait(driver, [Link](10));

[Link]([Link]([Link]("username")));

=============================
[Link] wait

Advanced Explicit wait.


Custom polling time

Code:-

Wait<WebDriver> wait = new FluentWait<>(driver)


.withTimeout([Link](20))
.pollingEvery([Link](2))
.ignoring([Link]);

You might also like