Skip to main content

XPath Examples for Selenium

Case Insensitive XPath Example

private By BY_checkbox(string rowValue) => By.XPath($"//*[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='{rowValue}']//ancestor::tr//div[contains(@class,'ck-checkbox')]//div");
//div[@class='visible menu transition']//span[text()='Inactive']

Another Dropdown Selection XPath Example

//span[contains(.,'Status')]/../..//descendant::div[@'class='ui selection dropdown']

XPath for Getting Parent's Parent Div and Following Input

//span[contains(.,'Domains')]/../..//descendant::input

Sibling Elements XPath Examples

//span[@class='label' and text()='First Name']/../following-sibling::div/div/input

//div[contains(@class,'cd tree')]//span[text()='Newsletters']/../preceding-sibling::span

Additional XPath Example in C#

var callerWindowHandle = WebDriver.CurrentWindowHandle;

var elems = _appMenuParentElem.FindElements(BY_appMenu_link);

var elem = elems.Single(x => x.Text.Trim() == app.GetDescription());

var calleeWindowHandle = new PopupWindowFinder(WebDriver).Click(elem);

//span[contains(@class, 'myclass') and text() = 'qwerty']

These examples demonstrate various XPath expressions for locating elements using Selenium. You can adapt and use these XPath expressions in your Selenium automation scripts as needed.