Selenium Design Patterns and Best Practices A Practitoner's Guide To Test Automation Using Selenium Selenium 2 Testing Tools: Beginner s Guide - Book there Selenium Simplified - book there Java Power tools ** Instant Selenium Testing Tools Starter Test automation using Selenium Interview Questions: Guide to Crack Selenium Automation Interviews Selenium Testing tools cookbook: CSS & Xpath selector are popular. Find elemnts by CSS is recomended over xpath d ue to its simplicity, speed and performance. We can inspect element with Firebug (we use in FF as in ff dev tool we dont have copy xpath), Chrome dev tool inspect element, Simillarlly for IE - Dev tool (pr ess F12). Locating elements is done using methods findElement or findElements provided by WebDriver and WebElement class findElement() returns web element based on specified search, throws NoSu chElementFoundException when no match found findElements() return list of web elements. Returns empty list when no m atch found. findElement takes By class as argument. it takes locator or query object . examples By ID : driver.findElement(By.id (<element ID>) (java), driver.f ind_element_by_id (<elementid>) (python) By Name : driver.findElement(By.name(<element name>), driver.fin d__element_by_name(<element name>) By ClassName : driver.findElement(By.className(<element class>)) By TagName : driver.findElement(By.tagName(<htmlTagName>)) By LinkText : driver.findElement(By.linkText(<linktext>)) - Loc ate link using its text By partial Link Text : driver.findElement(By.partialLinkText(<li nktext>)) By Css : driver.findElement(By.cssSelector(<css Selector>)) By Xpath : driver.findElement(By.xpath(<xpath query expression>) Using ID is most preffered way to identify elements. While processing DO M, browsers use id as preferred way for identify element. This provides fastest locator stratergy. We go for identification for name, for following reasons: Not all page elements have id specified when id values are dynamically generated. Name attribute may not be unique. When multiple elements are found, 1st element is returned (may not be what we are looking for) Class attribute is provided to apply css to an element. Se,enium will lo oks through DOM for matching elements and return first matching element. WebElement class also supports find methods that find child elements. e.g. WebElement div = driver.findElement(By.id("div1")); Weblement toplink = div.findElement(By linkText("top")); in short : WebElement toplink=driver.findElement(By.id("div1")). findElement(By.linkText("top")); Locating element by text can cause issues while testing applications in multiple locales. Using parametrized text locator value work in such applns. Tagname returns list of matching elements for the specific tag name. May not be reliable while locating individual elements as page may have multiple in
stances of these elements. Generally if we want to get number of rows in table,
we used this (just example). To identify by cssSelector, we can use absloute path or relative path. For absloute path, we have to give path by separating with space s (like html head title) or using greater than symbol to represent childs (like html > head > title). But this has limitiations as it is restricted to structure . Relative path, we give direct element name. Like By.cssSelector( "input"); it uses class and id attributes to locate element using relative paths . While finding element using cssSelector, We can use class attribute to locate element. this can b e done by adding type of html tag then a dot followed by value of class attribut e ********** check this again.Page 23 for ref. e.g. driver.findElement(By.cssSelector("input.lo gin")). This will find login buttons <input> tag with class attriure is login. in the aboue example, if we leave HTML tag and j ust gave (.login) then it will return all elements with class as login. similar to className locator. We can use ID of assigned element. Done using HTML tag f ollowed by # then value of class attriute. e.g. By.cssSelector("input#usename"). return inp ut element with it id attribute. if we skip HTML tag, then it will return all ele ments with the id set as username. same as id locator stratergy. It will also allow identifying elements using other attr ibutes of elements. e.g. By.cssSelector("input[name=username]") this is similar to name() locator mtd of the By class. By.cssSelector("img[alt='previous']"), l ocats element using alt attribute If we have to use multiple attributes for precis e match : "input[type='submit'][value='login']" One major diff between xpath and css is, using xpath we can trav erse back and forth. But with css we can only process forward.