Video summary
Selenium webdriver With Java Session#3 Part-1 (Basic Locators) in Hindi
Main summary
Key takeaways
Main ideas / concepts covered
-
Purpose of Selenium locators
- Locators are used to identify web elements on a page.
- Before any action (like click or sending keys), the element must be located using a locator.
-
Locator strategies (basic locating strategies)
- Common locator strategies include:
- By ID
- By Name
- By Class Name
- By Tag Name
- By Link Text
- By Partial Link Text
- By Text
- By Position (index/position-based reference)
- Fallback idea when elements can’t be located
- If basic locators don’t work, you can use more advanced approaches such as CSS/XPath-style “custom” locators.
- Common locator strategies include:
-
findElementvsfindElementsfindElement- Returns one element (the single match).
- If multiple matches exist, it typically returns the first matching element.
- If nothing matches, it can throw a NoSuchElementException.
findElements- Returns a list of all matching elements.
- If nothing matches, it returns an empty list (no exception).
- Key distinction:
- Return type (single element vs list)
- Behavior with multiple matches (first match vs all matches)
-
Practical example: Login automation
- Uses a sample webpage and performs:
- Launch Chrome
- Open a URL
- Locate:
- Username field
- Password field
- Login button
- Enter credentials using sendKeys
- Click the login button
- Uses a sample webpage and performs:
-
Choosing locators correctly (uniqueness matters)
- A locator strategy must be unique enough to target the correct element.
- Examples shown:
- Using
By nameorBy tag namemay lead to multiple matches, causing confusion or incorrect targeting. - If multiple elements share the same attribute (e.g., same
nameor same tag), Selenium may return the wrong one (especially withfindElement).
- Using
-
Practical example: Clicking product links
- Uses link-based locators to navigate to a product page:
- Locates links via link text / partial link text
- Highlights a common Selenium requirement:
- If navigation opens content in another window/tab, you must:
- Get window handles
- Switch to the correct window handle before clicking the target link.
- If navigation opens content in another window/tab, you must:
- Uses link-based locators to navigate to a product page:
-
Selecting by partial text safely
- Partial link text can be used as a substring instead of full exact text.
- If the partial text matches multiple links:
findElementmay click the first matching linkfindElementsreturns all matching links, letting you choose among them
-
Element list size usage
- Uses
findElementsand prints the list size to determine how many matching elements exist for a locator/text.
- Uses
Methodology / instruction-like steps (detailed)
A) Login flow using locators
- Setup
- Create a Java class for the Selenium test.
- Configure WebDriver for ChromeDriver.
- Open page
driver.get("http://...")(demo URL referenced in subtitles)
- Locate elements
- Locate username input using a chosen strategy (example uses By ID).
- Locate password input using a chosen strategy (example uses By name).
- Locate login button using a chosen strategy (example uses By class name).
- Enter credentials
usernameElement.sendKeys("...")passwordElement.sendKeys("...")
- Click login
loginButton.click()
B) Verify locator accuracy (avoid wrong targeting)
- When a locator strategy returns ambiguous results:
- If using
findElementand the locator matches multiple elements:- make the locator unique (e.g., use a more specific attribute like ID, or more specific text)
- If using
findElements:- collect all matches into a list and inspect them (e.g., check
list.size())
- collect all matches into a list and inspect them (e.g., check
- If using
C) Navigating via links + handling windows/tabs
- After login, locate and click a link that opens the product page.
- If the product opens in a new window/tab:
- Get all window handles:
driver.getWindowHandle(s)(handle retrieval referenced in subtitles) - Switch:
driver.switchTo().window(handle) - Then locate the product link and click it.
- Get all window handles:
D) Finding link(s) by text / partial text
- Use link text options:
- By Link Text: matches the link’s complete visible text
- By Partial Link Text: matches only a portion of visible text
- If partial text matches multiple links:
- Use
findElementif you’re okay with the first match - Use
findElementsif you need all matches
- Use
E) List-based approach with findElements
- Use:
List<WebElement> list = driver.findElements(<locator>);
- Then:
System.out.println(list.size());(to know how many elements/links match)
- Iterate selection logic (implied):
- choose the correct element based on expected position/text among the list
Speakers / sources featured (as identified in subtitles)
- Shailendra Chauhan (speaker/host)
- Subhash Gupta (mentioned)