Video summary

Selenium webdriver With Java Session#3 Part-1 (Basic Locators) in Hindi

Main summary

Key takeaways

Educational

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.
  • findElement vs findElements

    • findElement
      • 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:
      1. Launch Chrome
      2. Open a URL
      3. Locate:
        • Username field
        • Password field
        • Login button
      4. Enter credentials using sendKeys
      5. Click the login button
  • Choosing locators correctly (uniqueness matters)

    • A locator strategy must be unique enough to target the correct element.
    • Examples shown:
      • Using By name or By tag name may lead to multiple matches, causing confusion or incorrect targeting.
      • If multiple elements share the same attribute (e.g., same name or same tag), Selenium may return the wrong one (especially with findElement).
  • 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.
  • 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:
      • findElement may click the first matching link
      • findElements returns all matching links, letting you choose among them
  • Element list size usage

    • Uses findElements and prints the list size to determine how many matching elements exist for a locator/text.

Methodology / instruction-like steps (detailed)

A) Login flow using locators

  1. Setup
    • Create a Java class for the Selenium test.
    • Configure WebDriver for ChromeDriver.
  2. Open page
    • driver.get("http://...") (demo URL referenced in subtitles)
  3. 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).
  4. Enter credentials
    • usernameElement.sendKeys("...")
    • passwordElement.sendKeys("...")
  5. Click login
    • loginButton.click()

B) Verify locator accuracy (avoid wrong targeting)

  • When a locator strategy returns ambiguous results:
    • If using findElement and 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())

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.

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 findElement if you’re okay with the first match
    • Use findElements if you need all matches

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)

Original video