You are on page 1of 1
Locator Strategies in Selenium WebDriver Introduction [Now a cay Selenium is vastly used as webs automation tool acrose incur: thas ts cnn advantages whether might be an open sou sevigting language. Selenium WebDriver mimics the actual user operations. so it gives the actual user experience wile execution, Selenium nave wide range of locators which helps ta lacste elements on a web aaa Today we wil tlk about them. Selenium Locators Selenium gives user options to locate er in 9 cierent ways o + Name © Linton + Prtil Linktont Tag Name + Cass Name + C85 (cascades Spreads ‘= XPath cov patly = DOM (Data object modeling) (Net supported by WebDriver) ‘ne sequence of the operator saws hew much your script going t2 bs efficent while execution. Te elaborate thi lets take one example We wil try to locate field User ID by Id and XPath Locetorby id: Id= tatUserName Locator by spath: XPath= //inputl@ (path might have ciffarent cembination we wil ee them in sath ection) Thwe execute script using both locators. o locate the Feld using I wil take les time compared ta Xpath. In tis way we wil able to Increase the efficiency of, script by reducing execution time, Now We will eee all locators in data “extUserName') Selenium Locators: Locate element by Id ne most prefered the esi and eine way to locate an element on 8 web page By 1.18 wil he atibute onthe page wich wil be unique ke you bank acount umber or employee id Ks ate the safest and fastest actor option and should aktays be the fist choke even when there are mule ce Example 4: 1 straight forwars we only have ld, Buti second we have le a well as Name as an attnbute. We can write the script as ebelanent Ele = ceiver. findélenent (ly id(“rtUseriiane ")) 3 But in many cases we found that we have common Id or dynamic Ids (ike in case of google, Gmail or the application using GW/T.In that case we need to use different locators Selenium Locators: Locate element by Name This ia fall back option when Id fo element i nt present Bu mostly the names are sed again and again, so make sure thatthe na age before using it unique on the Example input “tetUserlane” nanea"useriiane” type="text”> ebElanent ele driver. finde-lenent (@y.nane(“userta Ni Selenium Locators: Locate element by LinkText Finding an element with intext very simple, This neater ie uted cae you want ta locate any hypernk only. But make ste, there only ane unique nk on the web page. there are mull inks with the same lnk, in such cases Selenium wil perform action on the fist matching element with nk on page. Example: In above image ws have tes hyparinks. we want to locate the Forgat Passware? Link, The locetr val Be Forgot Password? \ebElenent hyperlink = driven. findElenent(8y.LinkText( "Forgot Passnord?")); We have 2 inks with text Forgot Email. If we try to locate 2° ink wth locator forgot Emal. Sclenium wil locate fink Tin this case #we want to locate the 2 ink, we will ned to use exact keyword with colon (exmet), The locator in thst case wil be kre sacttFonget Email ca style-"background-colon: transparent” nref="#">*rongot Email ebElanent hyperlink = driven. indelenene(sy.Linktexe(“exact:*Forgot Ematl))s Selenium Locators: Locate element by Partial LinkText Paria LinkText works same as LinkText only ference is you can use apart ofthe text from link Forgot Passuord? \ebElenent hyperlink = driven.findelenent(ey. PartialLinktext ("Password”)): Selenium Locators: Locate element by Tag Name Tag Name we can use for the elements like drop downs check boxed, radio button. Following html fs for op down with 3 values. To select hat dropdown ‘we wll use taghame locato. Example eboriver command: Select select ~ now Select (deiver.Findelexont (By. tagitane("select")))s select. selectByvisibleText("Puna"); select select8yValue("PUE") ; Selenium Locators: Locate element by Class Name Tis locator we can use a a fal back option for either name ord But the same conltion applied here Class name should be unique or selenium wil locate the first element present on the page with the classname ws have used to locate element Example ‘input 1d-"ttNane” class~"textboxcss" tablndex"2" type~"text”> jenent (By cLasstane(“textboxcss”)); webElenent classtest ~delver. fin Selenium Locators: Locate element by CSS selector using html tag attributes This a fll back when all options fai, you can use parent child relation in tags in case you need to use complex strategy te locate elements. CSS selectors are stong representation of HTML tags, stributes Id, Class. t's somewhat complex strategy compared to the previous we sean, But we can locate the elements hich don’t have even Td or name or class using CSS selectors ‘We can use cliferent combinations of atributes to locate an element using CSS selector. + Tag and 1D 1 Tag and cass + Tag and attribute Tag, le and ateibute 1 Tag, class, and attribute + theta Inner text ot supported by webdrven “These ate commonly used combinations for more combinations you can refer this URL hil/favanuschoo! But you need to chack frst tat the combination yeu are going to use = supported By Selenium WebDriver. Tag_and ID Inthis case you need to follow this syntax eas=tagid For Id we need to use # sign before id val com/cssret/ess select exam “input id-"tethone” classa"texthoxces" tabindexc"" types"text”> npurttxt Name \MebElenent cssele = driver. Find lenants(y.cesSelecton( nputatxane”)) Here inputs tag name and 6 is baName with # sign Tag and Class Inthi case you need to follow this syntax css-tagclasname, For lass we need to use dofbefore clas valve. there i space between classname ike classname you need to use dot inbetween space. Example 1 sextboxces" tebindex<"1" typ nput textboxes \ebElenent cssele = driver. Findélenents (By .cssSelector( “input textboxcss")); "texthoxcss top” tabinde a" types"text™> MebElenent cssele = driver Find lenents(By.cssSelector(“input textboxess top"); Hate inputs tag name folloned by dot and class name textboxcss. In example 2 class name is textboxcsstop in that case we put det in between textboxcss and top. and Attribute In thie case wien w= don't have both Io or class name we go far html atibutes givan in tage Syntax for tis combination is exs=taglattribute="value', We need to use square brackets to specify the attrioute and its value. Put the value between single quotes when you are writing script in Java, exanple esseInput{type~checkbox?] cesenput{value~’ Reading? ] Herein first case type isatribute and checkboxs its value, in other case value i an attribute and its value Reading Tag, ID and Attribute Tn thi case when we have common id bu other atibues ae diferent we go with this combination, Syntax or this combination is css-tagiidattribute~'vale'L Example «ss-LnputrtxtNane[nane~’taCorment? ] webElenent cssele ~ driven Findélenents (By .cesSelecton("E iputtxtNane[nane—? tacomment?]")); Here for both textboxes id is same ever class name is also same, only name iscitferent for both So if we want to lecate fist text box we will go with locator ‘given in stove example, For second text box we need to change value of name attribute in same combination. Tag, Class and Attribute Inthis case when we have id but we have class name which is common around other elements but other atibutes are diferent we go with this combination. Syntax for this combination is es5=tag.classnamefattribute='value'] Example css-Lnputtextboxcss [nane-? tacoanent?] WobElonent cssele ~ driven Findelenents (By .cssSelecton("Input-textboxcss [nane-" acoaent? J") 5 nth-chilld(), In this case we have same ld or class name and other attributes for diferent elements, we can go with nth-childd Syntax for this combination is exs=taginth-child(a). Here in syrtax we can use any combination discussed above, With that we need to use: nth-chldfn) a represent child number. Example
    albecsi> abe aubcecsis eliplavag/8> eLippythone/ > Lipubye/> <> esse Henth-chila(n) ebElenent cssele = driven. Find lenents(8y.cssSelector(“1i:ath-child(n)")); Here ifwe can see the ul fi parent child structure, We have only tag names which are common to everyone, Here if we want to locate sat Java we will put in above commans Inner text This ie ight now not supported by WebDriver in case of CSS, but most probably will support in upcoming Selenium 3.0r 4 Syntax eas tag:containa( inner text), Hrs in syntax can use any combination discussed above, With that we neac!ta use () containe(nne® text Example case spanzcontains (‘Upload you pic *) WebElenent cssele = driver. Findélenents (By .cssSelector("span:contains( ‘Upload you pic*)")); Absolute and Relative Path The Examples we have seen bl now ate related to only single tag and its atnbute, But whan we cequie to build path using parent child relation we need fo sive ther absolute or relative path abc abe elbbpythone/> eu Consider here we want to locate Python using parent child relation. In that case relative path willbe Relative path: r=-divul’space> linth-cniel2) or cee~cli-espace>linty-chile(), ln sesond combination ws have removed ul Space denotes its 8 Relative path, Here WetDriver will search 3 inside given div and ul ‘And if we want Absolute path for same it will be Absolute path: s5= div>ul limth-child3) or ess=ul> linth-child(), Here angular bracket denotes Absolute path. Its an exact path for given element, Ta erow the dierence between both ats consider the example, Someone asked you where is your office? Most comman answers Hinlewadi. But it courier boy asked! you the address you will tl fll and exact address of your office. one is relative path to your office but 2"is absolute one.

You might also like