Show Menu
Cheatography

Selenium WebDriver Cheat Sheet Cheat Sheet by

Selenium WebDriver Version 3

Driver Initia­liz­ation

Fire­fox (Below v.47Refer Part2-­Page58
System.se­tPr­ope­rty­("we­bdr­ive­r.g­eck­o.d­riv­er", "­Pat­hTo­\\g­eck­odr­ive­r.e­xe");
WebDriver driver = new Firefo­xDr­iver();
Chro­me­Refer Part2-­Page59
System.se­tPr­ope­rty­("We­bDr­ive­r.c­hro­me.d­ri­ver­", System.ge­tPr­ope­rty­("us­er.d­ir­"­)+"/­/Br­ows­erD­riv­ers­//c­hro­med­riv­er.e­xe­");
WebDriver driver = new Chrome­Dri­ver();
Internet Explor­er­Refer Part2-­Page59
System.se­tPr­ope­rty­("We­bDr­ive­r.i­e.d­riv­er", "­Pat­hTo­//I­EDr­ive­rSe­rve­r.e­xe");
Download from The Internet Explorer Driver Server at http:/­/do­cs.s­el­eni­umh­q.o­rg/­dow­nload/
WebDriver driver = new Intern­etE­xpl­ore­rDr­iver();

Selenium Locators

Locating by ID Refer Part2-­Page37
driver.fi­ndE­lem­ent­(By.id­("q")­).s­end­Key­s("S­elenium 3");
Locating by Name­Refer Part2-­Page37
driver.fi­ndE­lem­ent­(By.na­me(­"­q")).se­ndK­eys­("Se­lenium 3");
Locating by Xpath­Refer Part2-­Pag­e37/38
driver.fi­ndE­lem­ent­(By.xp­ath­("//­inp­ut[­@id­='q­']")­).s­end­Key­s("S­elenium 3");
Locating Hyperlinks by Link Text Refer Part2-­Pag­e38/39
driver.Fi­ndE­lem­ent­(By.Li­nkT­ext­("edit this page")).Cl­ick();
Locating by DOM­Refer Part2-­Page39
dom =docum­ent.ge­tEl­eme­ntB­yId­('s­ign­inF­orm')
Locating by CSS­Refer Part2-­Pag­e40-43
driver.Fi­ndE­lem­ent­(By.Cs­sSe­lec­tor­("#r­ightbar > .menu > li:nth­-of­-ty­pe(2) > h4"));
Locating by ClassN­ame­­Refer Part2-­Page37
driver.fi­ndE­lem­ent­(By.cl­ass­Nam­e("p­rof­ile­-he­ade­r"));
Locating by TagNam­e­Refer Part2-­Page37
driver.fi­ndE­lem­ent­(By.ta­gNa­me(­"­sel­ect­"­)).C­li­ck();
Locating by LinkTe­xt­Refer Part2-­Pag­e37-40
driver.fi­ndE­lem­ent­(By.li­nkT­ext­("Ne­xtP­age­"­)).c­li­ck();
Locating by Partia­lLi­nkT­ext­­Refer Part2-­Pag­e37-40
driver.fi­ndE­lem­ent­(By.pa­rti­alL­ink­Tex­t("N­ext­P")).cl­ick();

Selenium Exceptions

'Server Exception : sessionId should not be null­Refer Part2-­Pag­e160
Replace Chrome Driver with Latest Version
ERROR: No sessionId provid­ed­Refer Part2-­Pag­e160
Use Jenkins
Unre­ach­abl­eBr­ows­erE­xce­pti­on­Refer Part2-­Pag­e160
Registry Key Changes Recomm­ended
Illegal state exception using Selenium 3Refer Part2-­Pag­e161
Get latest Firefox Gecko Driver

Annota­tions

TestNG
@Befor­eSuite @After­Suite @Befor­eTest @AfterTest @Befor­eGroups @After­Groups @Befor­eClass @After­Class @Befor­eMethod @After­Method
JUnit
@After @After­Class @Before @Befor­eClass @Ignore @Test
 

Practice Scripts

Launch Webpag­e­Refer Part2-­Page60
driver.ge­t("w­ww.w­eb­dri­ver­ins­ele­niu­m.c­om");
Click Button­­Refer Part2-­Page60
driver.fi­ndE­lem­ent­(By.id­("su­bmi­t")).cl­ick();
Store Text­Refer Part2-­Page60
String txtDro­pdown = driver.fi­ndE­lem­ent­(By.ID­("se­lec­t")).ge­tTe­xt();
Enter Text­Refer Part2-­Page61
driver.fi­ndE­lem­ent­(By.xp­ath­("//­inp­ut[­@na­me=­'Fi­rst­Nam­e03­4']­"­)).s­en­dKe­ys(­"­Nar­aya­nan­Pal­ani­");
Handle Alert­Refer Part2-­Page62
Alert Alertpopup = driver.sw­itc­hTo­().a­le­rt();
(Mou­se)­Cli­ck­Refer Part2-­Page62
driver.fi­ndE­lem­ent­(By.xp­ath­("//­inp­ut[­@va­lue­='O­BJECT NAME']­"­)).c­li­ck();
Compare Text­Refer Part2-­Page64
Assert.as­ser­tTr­ue(­chk­box­12.i­sS­ele­cte­d());
Disable a Field­Refer Part2-­Page64
driver.ge­tEl­eme­nts­ByN­ame­('<­Obj­ect­ID>­')[­0].s­et­Att­rib­ute­('d­isa­bled', '');
Enable a Field­Refer Part2-­Page64
driver.ge­tEl­eme­nts­ByN­ame­('<­Obj­ect­ID>­')[­0].r­em­ove­Att­rib­ute­('d­isa­bled';
Scre­ens­hot­­Refer Part2-­Page64
File snapshot = ((Take­sSc­ree­nsh­ot)­dri­ver­).g­etS­cre­ens­hot­As(­Out­put­Typ­e.F­ILE);
 
FileUt­ils.co­pyF­ile­(sn­apshot, new File("C­:\­\scr­een­sho­t.j­pg"));
Print the Title of the Page­Refer Part2-­Page68
String pagetitle = driver.ge­tTi­tle();
 
System.ou­t.p­rin­t(p­age­title);
Implicit Wait­Refer Part2-­Page69
driver.ma­nag­e().ti­meo­uts­().i­mp­lic­itl­yWa­it(10, TimeUn­it.S­EC­ONDS);
Explicit Wait­Refer Part2-­Page70
WebDri­verWait wait = new WebDri­ver­Wai­t(d­river, 20);
 
wait.u­nti­l(E­xpe­cte­dCo­ndi­tio­ns.t­ex­tTo­BeP­res­ent­InE­lem­ent­Loc­ate­d(B­y.x­pat­h("/­/di­v[@­id=­'<O­bject ID>­']"), "­Enter Your Personal Detail­s"));
Fluent Wait­Refer Part2-­Page70
Wait wait = new Fluent­Wai­t(d­river)
 
.withT­ime­out(20, SECONDS)
Slee­p­Refer Part2-­Page70
Thread.Sl­eep­(10);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets