Tuesday, September 27, 2022

Selenium locators using Xpath,css, id, classname, name, tagname, linktext

 selenium supports locators:


Xpath,css, id, classname, name, tagname,  linktext

id --- id field of the individual html tags/elements




driver.FindElement(By.Id("username")).SendKeys("rahulshettyacademy");

driver.FindElement(By.Id("username")).Clear();


//by tagname 

driver.FindElement(By.CssSelector("input[value='Sign In']")).Click();

//by xpath

//  //tagName[@attribure = 'value']

driver.FindElement(By.XPath("//input[@value='Sign In']")).Click();





to see the error messages.


lets start with timeout

Thread.Sleep(3000); //to wait for error message to appear

String errorMessage = driver.FindElement(By.ClassName("alert-danger")).Text;

TestContext.Progress.WriteLine(errorMessage);


//linktext only works for anchor tag.

IWebElement link = driver.FindElement(By.LinkText("Free Access to Interview Qs"));

String hrefAttr = link.GetAttribute("href");

String expectedUrl = "https://google.com"

Assert.AreEqual(expectedUrl, hrefAttr);


//xpath can be used to traverse child elements



extension chrome: SelectorsHub

driver.FindElement(By.XPath("//div[@class='form-group'][5]/label/span/input")).Click();


//by css selector by id and by class name



based on id, #signInBtn

in xpath selectorshub, test: .text-info span input

inside text-nfo class, span element inside input element.

.text-info span:nth-child(1) input






No comments:

Post a Comment