4 Hour Topics
linkText and partialLinkText
🔍 What They Do
By.linkText("Exact Text")
Targets<a>elements whose visible text exactly matches"Exact Text". It’s case-sensitive and needs the full text. lambdatest.com+11browserstack.com+11stackoverflow.com+11By.partialLinkText("Some Substring")
Selects the first<a>whose visible text contains"Some Substring". Useful when links are long, dynamic, or you only know part of the text. It’s also case-sensitive.🧠 Why
linkTextMight FailIf the link’s text is wrapped in nested elements (e.g.,
<strong>,<small>,<span>),linkText()expects the full, exact combined text. Example from Wikipedia English link:<a> <strong>English</strong> <small>5 472 000+ articles</small> </a>Calling
By.linkText("English")fails, because the full text is"English5 472 000+ articles". But By.partialLinkText(“English”) works, as it only needs a substring match.
📋 Example Usage
Java
driver.get("https://example.com");
// Exact match:
WebElement link = driver.findElement(By.linkText("Sign Up"));
link.click();
// Substring match:
WebElement partial = driver.findElement(By.partialLinkText("Sign"));
partial.click();
XPATH
XPath (XML Path Language) is a query language designed to navigate XML/HTML document trees. In Selenium, By.xpath("...") lets you target elements via these powerful expressions.
🔎 1. Absolute vs Relative XPath
Absolute XPath starts from the document root (e.g.
/html/body/div[1]/form/input) – highly brittle.Relative XPath (e.g.
//form[@id='loginForm']//input[@name='username']) is more robust and recommended.✨ 2. Core Syntax, Axes & Functions
XPath Steps
//tag[@attr='value'] → any <tag> with that attribute //tag[text()='Exact'] → match text exactly //tag[contains(@class,'btn')] → partial attribute match //tag[starts-with(@id,'pre')] → match prefix🔄 1. Navigating with XPath Axes
XPath axes let you traverse the DOM based on relationships instead of complex relative paths.
ancestor::Selects parent, grandparent, and all higher-level nodes of the current element.
//span[@class='price']/ancestor::div[@class='item']Finds a
<div class="item">wrapping the price span.parent::Goes just one level up to the immediate parent.
//input[@id='searchBox']/parent::formTargets the
<form>containing the search box.child::Grabs direct children of the context node.
//ul[@id='menu']/child::liReturns each
<li>directly under the<ul id="menu">.following-sibling::Selects siblings after the current node.
//h2[text()='Section A']/following-sibling::pFinds paragraphs after the heading "Section A".
preceding-sibling::Selects siblings before the current node.
//h2[text()='Section B']/preceding-sibling::pGrabs the paragraphs before "Section B".
🔗 2. Combining Conditions with AND/OR
Use
and/orin predicates to match multiple conditions://button[@type='submit' and normalize-space(text())='Send']This finds a
<button>that is both a submit button and contains exactly "Send”. Useful when single attribute or text isn't unique.
🟢 1. Union (| or union)
What it does: Combines two node sets, picks every node that's in either one.
Behavior: Removes duplicates and sorts results in document order.
Example:
//div | //span
➜ Selects all
<div>and<span>elements, with no repetition.
🔵 2. Intersect (intersect)
What it does: Finds nodes that appear in both node sets.
Behavior: Intersection of two lists of nodes, sorted, no duplicates.
Example:
(//section | //article) intersect //*[@id='main']
➜ Picks nodes that are both
<section>or<article>and have id="main".
🔴 3. Except (except)
What it does: Takes nodes from the first set that are not present in the second set.
Behavior: Think "minus" or "difference"—returns A - B. Sorted, no duplicates.
Example:
//a except //a[@class='external']
➜ Selects all links (<a>) except those having class="external".
CSS Selectors
CSS selectors in Selenium are powerful patterns (borrowed from CSS styling) that let you locate web elements efficiently by matching their tag names, IDs, classes, attributes, relationships, and more. They’re often faster and more concise than XPath. Let’s break it down 👇
🔍 Basic Types of CSS Selectors
ID Selector
Syntax:
#myIdortag#myIdExample:
driver.findElement(By.cssSelector("#loginBtn"))
Class Selector
Syntax:
.myClassortag.myClassExample:
driver.findElement(By.cssSelector(".menu-item"))
Attribute Selector
Syntax:
[attribute='value']or combined with tag:tag[attribute='value']Example:
input[type='email']
⚙️ Combined & Advanced Selectors
- Combine multiple attributes:
button[type='submit'][name='login']
Attribute Selectors
In CSS Selectors used in Selenium, you can locate elements using attribute selectors by targeting HTML attributes like type, name, placeholder, etc. The syntax is straightforward:
[attribute='value'] /* Select any element with the attribute=value */
tag[attribute='value'] /* Select a specific tag with that attribute=value */
🔹 Examples:
1. Locate an input with type 'email':
driver.findElement(By.cssSelector("input[type='email']"));
4. Locate a link with a specific href:
driver.findElement(By.cssSelector("a[href='/home']"));
🔸 Advanced Attribute Selectors:
| Selector | Description | Example |
[attr='val'] | Exact match | input[type='text'] |
[attr^='val'] | Starts with | input[name^='user'] |
[attr$='val'] | Ends with | input[id$='name'] |
[attr*='val'] | Contains | input[placeholder*='email'] |
💡 In Selenium Java:
WebElement emailInput = driver.findElement(By.cssSelector("input[type='email']"));