When performing website automation testing, using the right locator strategy is critical for creating robust, maintainable, and reliable tests. Below is an overview of the most effective locator strategies in Selenium, Playwright, or other web automation tools:
Locators:
Id,
Xpath,
Css selector,
Name,
Class name,
Tag name,
Link text,
Partial link text
CSS Selectors constructing: By.cssSelector()
- tagname.classname
- tagname#id
- tagname[attribute=‘value’]
- [attribute=‘value’]
- Tagname[@attribute=’value’]:nth-child(index)
- parentTagName childTagName
- [class=‘parentClass’] [class=‘childClass’] Eg: (By.cssSelector("[class='datepicker-days'] [class='datepicker-switch']"))
- Regular expression for contains value keyword: tagName[attribute*=‘value’]
- Tagname
Note: If the class in tag contains multiple classes, just construct cssSelectors as by removing spaces and replace with dot.
Eg: By.cssSelector(“.classname1.classname2.classname3”)
By.cssSelector("tagname");
By.cssSelector("#id");
By.cssSelector(".classname");
By.cssSelector("tagname[attribute='value']");
Starts with: driver.findElement(By.cssSelector("a[href^='https']"));
Ends with: driver.findElement(By.cssSelector("img[src$='.png']"));
Contains: driver.findElement(By.cssSelector("input[name*='username']"));
Combining selectors:
Descendant selector: By.cssSelector("parent child"); driver.findElement(By.cssSelector("div form"));
Direct child: By.cssSelector("parent > child"); driver.findElement(By.cssSelector("ul > li"));
Multiple classes: By.cssSelector("tagname.class1.class2");
Grouping selector: By.cssSelector("selector1, selector2"); driver.findElement(By.cssSelector("button, input[type='submit']"));
Pseudo classes:
First child: By.cssSelector("parent :first-child"); driver.findElement(By.cssSelector("ul > li:first-child"));
Last child: By.cssSelector("parent :last-child"); driver.findElement(By.cssSelector("ul > li:last-child"));
Nth child: By.cssSelector("parent :nth-child(n)"); driver.findElement(By.cssSelector("table tr:nth-child(2)"));
Contains Text CSS Selectors do not have a direct equivalent to XPath’s contains(text(), 'value'). Use XPath in such cases.
Note: Use browser dev tools to verify your CSS selectors (document.querySelectorAll("cssSelector");).
Xpath constructing:
- tagname[@attribute=‘value’]
- tagname[@attribute=‘value’][index]
- //parentTagName/childTagName[index] Eg: By.xpath(“//table/tbody/tr/td[1]/ui”)
- Regular expression for contains value keyword: //tagName[contains(@attribute,’value’)]
- //htmlTag[@attribute=‘value’]/tagName[index]
- //tagname
- //*[text()=‘Log Out’] —> tagname is regex * can be anything. EXAMPLE: //div[@class='ola-techPack-plan-buttons']//div[text()='Buy later']
- Sibling to sibling traverse:
- A. //header/div/button[1]/following-sibling::button[1]
- B. //header/div/button[2]/preceding-sibling::button[1]
- Child to parent traverse: //header/div/button[1]/parent::div/parent::header
- Parent-child relationship: //parentXpath //childXpath
Xpath:
Absolute Xpath:
/html/body/header
Relative Xpath:
//header/div/button[1]
Relative Locators(selenium 4 and above):
Df
Synchronisation in Selenium:
Implicit Wait
Explicit Wait: WebDriverWait, Fluent Wait
Thread.sleep
Actions:
How to mouseover on object with selenium
Performing mouse and keyboard interactions with selenium
Contextclick(right click) on element
Double click on element
Drag and dropping the element
Frames:
No comments:
Post a Comment