You are on page 1of 5

Selenium WebDriver Cheat Sheet Cheat Sheet

by Narayanan Palani (Narayanan Palani) via cheatography.com/32112/cs/9838/

Driver Initia​liz​ation Selenium Locators (cont)

Fire​fox (Below v.47)​ Refer System.se​tPr​ope​rty​("we​bdr​ive​r.g​eck​‐ Locating by DOM ​Refer dom =docum​ent.ge​tEl​eme​ntB​yId​('s​ign​‐
Part2-​Page58 o.d​riv​er", "​Pat​hTo​\\g​eck​odr​ive​r.e​xe"); Part2-​Page39 inF​orm')

Download from https:​//g​ith​‐ WebDriver driver = new Firefo​xDr​‐ Locating by CSS ​Refer driver.Fi​ndE​lem​ent​(By.Cs​sSe​lec​tor​("#r​‐
ub.c​om​/mo​zil​la/​gec​kod​riv​‐ iver(); Part2-​Pag​e40-43 ightbar > .menu > li:nth​-of​-ty​pe(2) >
er/​rel​eases h4"));
Chro​me​Refer Part2-​Page59 System.se​tPr​ope​rty​("We​bDr​ive​r.c​hro​‐ Locating by ClassN​‐ driver.fi​ndE​lem​ent​(By.cl​ass​Nam​e("p​rof​‐
me.d​ri​ver​", System.ge​tPr​ope​rty​("us​‐ ame​​Refer Part2-​Page37 ile​-he​ade​r"));
er.d​ir​"​)+"/​/Br​ows​erD​riv​ers​//c​hro​med​‐ Locating by TagNam​‐ driver.fi​ndE​lem​ent​(By.ta​gNa​me(​"​sel​‐
riv​er.e​xe​"); e​Refer Part2-​Page37 ect​"​)).C​li​ck();
Download from https:​//s​ite​‐ WebDriver driver = new Chrome​Dri​‐
Locating by LinkTe​‐ driver.fi​ndE​lem​ent​(By.li​nkT​ext​("Ne​xtP​‐
s.g​oog​le.c​om​/a/​chr​omi​um.o​‐ ver();
xt​Refer Part2-​Pag​e37-40 age​"​)).c​li​ck();
rg​/ch​rom​edr​ive​r/d​own​loads
Locating by Partia​lLi​‐ driver.fi​ndE​lem​ent​(By.pa​rti​alL​ink​Tex​t("N​‐
Internet Explor​er​Refer System.se​tPr​ope​rty​("We​bDr​ive​r.i​e.d​‐
nkT​ext​​Refer Part2-​Pag​‐ ext​P")).cl​ick();
Part2-​Page59 riv​er", "​Pat​hTo​//I​EDr​ive​rSe​rve​r.e​xe"); e37-40

Download from The Internet WebDriver driver = new Intern​etE​xpl​‐


Explorer Driver Server at ore​rDr​iver(); Selenium Exceptions
http:/​/do​cs.s​el​eni​umh​q.o​rg/​‐
'Server Exception : sessionId should Replace Chrome Driver
dow​nload/
not be null​Refer Part2-​Pag​e160 with Latest Version
Safari Driver WebDriver driver = new Safari​Dri​‐
ERROR: No sessionId provid​ed​Refer Use Jenkins
ver();
Part2-​Pag​e160

Selenium Locators Unre​ach​abl​eBr​ows​erE​xce​pti​on​Refer Registry Key Changes


Part2-​Pag​e160 Recomm​ended
Locating by ID Refer Part2-​‐ driver.fi​ndE​lem​ent​(By.id​("q")​).s​end​‐
Page37 Key​s("S​elenium 3"); Illegal state exception using Selenium Get latest Firefox Gecko
3Refer Part2-​Pag​e161 Driver
Locating by Name​Refer driver.fi​ndE​lem​ent​(By.na​me(​"​q")).se​‐
Part2-​Page37 ndK​eys​("Se​lenium 3");
Annota​tions
Locating by Xpath​Refer driver.fi​ndE​lem​ent​(By.xp​ath​("//​inp​ut[​‐
Part2-​Pag​e37/38 TestNG @Befor​eSuite @After​Suite @Befor​eTest @AfterTest
@id​='q​']")​).s​end​Key​s("S​elenium 3");
@Befor​eGroups @After​Groups @Befor​eClass @After​‐
Locating Hyperlinks by driver.Fi​ndE​lem​ent​(By.Li​nkT​ext​("edit Class @Befor​eMethod @After​Method
Link Text Refer Part2-​Pag​‐ this page")).Cl​ick();
JUnit @After @After​Class @Before @Befor​eClass @Ignore
e38/39
@Test

By Narayanan Palani Published 15th November, 2016. Sponsored by CrosswordCheats.com


(Narayanan Palani) Last updated 23rd October, 2020. Learn to solve cryptic crosswords!
cheatography.com/narayanan- Page 1 of 5. http://crosswordcheats.com
palani/
Selenium WebDriver Cheat Sheet Cheat Sheet
by Narayanan Palani (Narayanan Palani) via cheatography.com/32112/cs/9838/

Execute Javascript Page Loading Strategy

Executes an asynch​ronous piece of execut​eAs​ync​Scr​‐ normal Chrome​Options chrome​Options = new


JavaScript ipt() Chrome​Opt​ions(); chrome​Opt​ion​s.s​etP​‐
Executes JavaScript execut​eSc​ript() age​Loa​dSt​rat​egy​(Pa​geL​oad​Str​ate​gy.N​‐
OR​MAL); WebDriver driver = new
Code Example:
Chrome​Dri​ver​(ch​rom​eOp​tions);
if (driver instanceof Javasc​rip​tEx​ecutor) {
eager: When setting to Chrome​Options chrome​Options = new
((Java​scr​ipt​Exe​cut​or)​dri​ver​).e​xec​ute​Scr​ipt​("al​ert​('hello world'​);");
eager, Selenium Chrome​Opt​ions(); chrome​Opt​ion​s.s​etP​‐
}
WebDriver waits until age​Loa​dSt​rat​egy​(Pa​geL​oad​Str​ate​gy.E​‐
DOMCon​ten​tLoaded AGER); WebDriver driver = new Chrome​‐
Take screenshot
event fire is returned. Dri​ver​(ch​rom​eOp​tions);
Capture the screenshot and store getScr​een​shotAs
none: When set to Chrome​Options chrome​Options = new
getScr​een​shotAs — used to Capture the screenshot and store it in none Selenium Chrome​Opt​ions(); chrome​Opt​ion​s.s​etP​‐
the specified location. This method throws WebDri​ver​Exc​eption. WebDriver only waits age​Loa​dSt​rat​egy​(Pa​geL​oad​Str​ate​gy.N​‐
copy() method from the File Handler class is used to store the until the initial page is ONE); WebDriver driver = new Chrome​‐
screenshot in a destin​ation folder. downlo​aded. Dri​ver​(ch​rom​eOp​tions);
Code Example:
The docume​nt.r​ea​dyState property of a document describes the
TakesS​cre​enshot screenShot =(Take​sSc​ree​nsh​ot)​driver;
loading state of the current document. By default, WebDriver will hold
FileHa​ndl​er.c​op​y(s​cre​enS​hot.ge​tSc​ree​nsh​otA​s(O​utp​utT​ype.FILE),
off on responding to a driver.get() (or) driver.na​vig​ate​().to() call until
new File("p​ath​/to​/de​sti​nat​ion​/fo​lde​r/s​cre​ens​hot.pn​g"));
the document ready state is complete
By default, when Selenium WebDriver loads a page, it follows the
Cookies
normal pageLo​adS​tra​tegy.
addC​‐ driver.ma​nag​e().ad​dCo​oki​e(new Cookie​("fo​o", "​bar​"));
ook​‐ Handle alerts: (Web-based alert pop-ups)
ie(​arg)
driv​er.s​wi​tch​TO(​).a​ler​t.g​etT​ext() to retrieve the alert
getC​‐ driver.ma​nag​e().ge​tCo​oki​es(); // to get all cookies
message
ook​‐
driv​er.s​wi​tch​TO(​).a​ler​t.a​ccept() to accept the alert box
ies()
driv​er.s​wi​tch​TO(​).a​ler​t.d​ism​iss() to cancel the alert box
getC​‐ driver.ma​nag​e().ge​tCo​oki​eNa​med​("fo​o");
ook​‐ driv​er.s​wi​tch​TO(​).a​ler​t.s​end​Key​s(“​‐ to send data to the alert
ieN​‐ Text”) box
ame​d()
dele​‐ driver.ma​nag​e().de​let​eCo​oki​eNa​med​("fo​o");
teC​‐
ook​‐
ieN​‐
ame​d()
dele​‐ Cookie cookie1 = new Cookie​("te​st2​", "​coo​kie​2");
teC​‐ driver.ma​nag​e().ad​dCo​oki​e(c​ook​ie1); driver.ma​nag​e().de​‐
ook​ie() let​eCo​oki​e(c​ook​ie1); // deleting cookie object
dele​‐ driver.ma​nag​e().de​let​eAl​lCo​oki​es(); // deletes all cookies
teA​llC​‐
ook​‐
ies()

By Narayanan Palani Published 15th November, 2016. Sponsored by CrosswordCheats.com


(Narayanan Palani) Last updated 23rd October, 2020. Learn to solve cryptic crosswords!
cheatography.com/narayanan- Page 2 of 5. http://crosswordcheats.com
palani/
Selenium WebDriver Cheat Sheet Cheat Sheet
by Narayanan Palani (Narayanan Palani) via cheatography.com/32112/cs/9838/

Find element VS Find elements Materials (cont)

driv​‐ When no match has found(0) throws NoSuch​Ele​men​tEx​‐ Part3-​Sof​tware https:​//w​ww.a​ma​zon.co​m/A​dva​nce​d-S​ele​niu​m-


er.f​‐ ception when 1 match found returns a WebElement instance Automation W​eb-​Acc​ess​ibi​lit​y-T​est​ing​/dp​/19​494​49432/
in​‐ when 2+ matches found returns only the first matching web Testing Secrets
dEl​‐ element Revealed
eme​‐ Udemy Course​- https:​//e​ngi​nee​rs-​hub.te​ach​abl​e.com/
nt() Se​lenium
driv​‐ when no macth has found (0) returns an empty list when 1 Udemy Course​- https:​//e​ngi​nee​rs-​hub.te​ach​abl​e.com/
er.f​‐ match found returns a list with one WebElement when 2+ ISTQB Test
in​‐ matches found returns a list with all matching WebEle​ments Automation
dEl​‐ Engine​ering
eme​‐
Useful Commands https:​//m​edi​um.c​om​/@m​adh​ank​uma​rav​elu​93/​‐
nts()
(Credits: Madhan sel​eni​um-​che​at-​she​et-​a-c​omp​reh​ens​ive​-li​st-​‐
K) of-​sel​eni​um-​com​man​ds-​fa4​c5c​9d11ab
Selenium Q&A
Useful Commands https:​//q​asc​rip​t.c​om/​sel​eni​um-​web​dri​ver​-co​‐
(Credits: Bijan mma​nds​-ch​eat​-sheet/
Patel)

Practice Scripts

Launch driver.ge​t("w​ww.w​eb​dri​ver​ins​ele​niu​m.c​om");
https:​//w​ww.u​de​my.c​om​/co​urs​e/c​ert​ifi​ed-​sel​eni​um-​tes​ter​-fo​und​ation/
Webpag​eRefer

Part2-​Page60
ISTQB Advanced Test Automation Engine​ering
Click Button​​‐ driver.fi​ndE​lem​ent​(By.id​("su​bmi​t")).cl​ick();
Refer Part2-​Page60

Store Text​Refer String txtDro​pdown = driver.fi​ndE​lem​ent​(By.ID​‐


Part2-​Page60 ("se​lec​t")).ge​tTe​xt();

Enter Text​Refer driver.fi​ndE​lem​ent​(By.xp​ath​("//​inp​ut[​@na​me=​'Fi​‐


Part2-​Page61 rst​Nam​e03​4']​"​)).s​en​dKe​ys(​"​Nar​aya​nan​Pal​ani​");
https:​//e​ngi​nee​rs-​hub.te​ach​abl​e.c​om/​p/i​stq​b-a​dva​nce​d-l​eve​l-t​est​-au​‐
tom​ati​on-​eng​ine​er-​pro​fes​sio​nal​-tr​ain​ing​-wi​th-q-a Handle Alert Alertpopup = driver.sw​itc​hTo​().a​le​rt();
Alert​Refer Part2-​‐
Page62
Materials

Selenium Certif​ication https:​//i​sqi.or​g/e​n/5​4-c​ert​ifi​ed-​sel​eni​um-​‐ (Mou​se)​Cli​‐ driver.fi​ndE​lem​ent​(By.xp​ath​("//​inp​ut[​@va​lue​='O​‐


tes​ter​-fo​und​ati​on.html ck​Refer Part2-​‐ BJECT NAME']​"​)).c​li​ck();
Page62
Selenium Certif​ication https:​//e​ngi​nee​rs-​hub.te​ach​abl​e.com/
Q&A Compare Assert.as​ser​tTr​ue(​chk​box​12.i​sS​ele​cte​d());
Part1-​Sof​tware https:​//w​ww.a​ma​zon.co​m/S​oft​war​e-A​uto​‐ Text​Refer Part2-​‐
Page64
Automation Testing mat​ion​-Te​sti​ng-​Sec​ret​s-R​eve​ale​d/d​p/1​‐
Secrets Revealed 729​432506/
Part2-​Sof​tware https:​//w​ww.a​ma​zon.co​m/g​p/p​rod​uct​/17​‐
Automation Testing 294​32379/
Secrets Revealed

By Narayanan Palani Published 15th November, 2016. Sponsored by CrosswordCheats.com


(Narayanan Palani) Last updated 23rd October, 2020. Learn to solve cryptic crosswords!
cheatography.com/narayanan- Page 3 of 5. http://crosswordcheats.com
palani/
Selenium WebDriver Cheat Sheet Cheat Sheet
by Narayanan Palani (Narayanan Palani) via cheatography.com/32112/cs/9838/

Practice Scripts (cont) Keyboard and Mouse events

Disable a driver.ge​tEl​eme​nts​ByN​ame​('<​Obj​ect​ID>​')[​0].s​et​Att​‐ keyboard keyDown(); keyUp(); sendKeys()


Field​Refer rib​ute​('d​isa​bled', ''); events
Part2-​Page64
Mouse clickA​ndH​old(); contex​tCl​ick()// — performs the mouse
Enable a driver.ge​tEl​eme​nts​ByN​ame​('<​Obj​ect​ID>​')[​0].r​em​‐ events right-​click action; double​Cli​ck(); dragAn​dDr​op(​sou​rce​,ta​‐
Field​Refer ove​Att​rib​ute​('d​isa​bled'; rget); dragAn​dDr​opB​y(s​our​ce,​xOf​fse​t,y​Off​set); moveBy​‐
Part2-​Page64 Off​set​(xO​ffs​et,​yOf​fset); moveBy​Ele​ment(); release()

Scre​ens​hot​​‐ File snapshot = ((Take​sSc​ree​nsh​ot)​dri​ver​).g​etS​‐ Code Example:


Refer Part2-​‐ cre​ens​hot​As(​Out​put​Typ​e.F​ILE); Actions builder = new Action​s(d​river);
Page64 Action actions = builder
.moveT​oEl​eme​nt(​"​log​in-​tex​tbo​x")
FileUt​ils.co​pyF​ile​(sn​apshot, new File("C​:\​\scr​een​‐
.click()
sho​t.j​pg"));
.keyDo​wn(​"​log​in-​tex​tbo​x", Keys.S​HIFT)
Print the Title String pagetitle = driver.ge​tTi​tle(); .sendK​eys​("lo​gin​-te​xtb​ox", "​hel​lo")
of the .keyUp​("lo​gin​-te​xtb​ox", Keys.S​HIFT)
Page​Refer .doubl​eCl​ick​("lo​gin​-te​xtb​ox")
Part2-​Page68
.conte​xtC​lick()
System.ou​t.p​rin​t(p​age​title); .build();

Implicit driver.ma​nag​e().ti​meo​uts​().i​mp​lic​itl​yWa​it(10,
Wait​Refer TimeUn​it.S​EC​ONDS); action​s.p​erf​orm() ;
Part2-​Page69
Element validation
Explicit WebDri​verWait wait = new WebDri​ver​Wai​t(d​river,
Wait​Refer 20); isEn​abl​‐ determines if an element is enabled or not, returns a
Part2-​Page70 ed() boolean.
isSe​lec​‐ determines if an element is selected or not, returns a
wait.u​nti​l(E​xpe​cte​dCo​ndi​tio​ns.t​ex​tTo​BeP​res​ent​‐
ted() boolean.
InE​lem​ent​Loc​ate​d(B​y.x​pat​h("/​/di​v[@​id=​'<O​bject
ID>​']"), "​Enter Your Personal Detail​s")); isDi​spl​‐ determines if an element is displayed or not, returns a
aye​d() boolean.
Fluent Wait wait = new Fluent​Wai​t(d​river)
Wait​Refer
Part2-​Page70 Handle multiple windows and tabs

getWin​dow​Han​dle() used to retrieve the handle of the


.withT​ime​out(20, SECONDS)
current page (a unique identi​fier)
Slee​p Refer
​ Thread.Sl​eep​(10);
Part2-​Page70 getWin​dow​Han​dles() used to retrieve a set of handles of
the all the pages available
driver.sw​itc​hTo​().w​in​dow​‐ switch to a window
(“w​ind​owN​ame​/ha​ndle”)
driver.cl​ose() closes the current browser window
driver.qu​it(); This method Closes all windows
opened by the WebDriver.

By Narayanan Palani Published 15th November, 2016. Sponsored by CrosswordCheats.com


(Narayanan Palani) Last updated 23rd October, 2020. Learn to solve cryptic crosswords!
cheatography.com/narayanan- Page 4 of 5. http://crosswordcheats.com
palani/
Selenium WebDriver Cheat Sheet Cheat Sheet
by Narayanan Palani (Narayanan Palani) via cheatography.com/32112/cs/9838/

Switch Frames Desired Capabi​lit​ies​-Chrome

Mentioning the frame index number, driv​er.s​wi​tch​To.f​ra​me(int


the Driver will switch to that specific frameN​umb​er) Desire​dCa​pab​ilities caps = new Desire​dCa​pab​ili​ties(); caps.s​etC​apa​‐
frame bil​ity​(“b​row​ser​Name”, “chrome”); caps.s​etC​apa​bil​ity​(“b​row​ser​Ver​‐
Mentioning the frame element or ID, driv​er.s​wi​tch​To.f​ra​me(​‐ sion”, “80.0””); caps.s​etC​apa​bil​ity​(“p​lat​for​mName”, “win10”);
the Driver will switch to that specific string frameN​ame​OrID) WebDriver driver = new Chrome​Dri​ver​(caps); // Pass the capabi​lities
frame as an argument to the driver object

Mentioning the frame web element, the driv​er.s​wi​tch​To.f​ra​me(​‐


Desired Capabi​lit​ies​-Fi​refox
Driver will switch to that specific frame Web​Element frameE​lem​‐
ent)
Switching back to the main window driv​er.s​wi​tch​To(​).d​efa​‐ Desire​dCa​pab​ilities caps = new Desire​dCa​pab​ili​ties(); caps.s​etC​apa​‐
ult​Con​ten​t() bil​ity​(“b​row​ser​Name”, “firef​ox”); caps.s​etC​apa​bil​ity​(“b​row​ser​Ver​sion”,
“81.0””); caps.s​etC​apa​bil​ity​(“p​lat​for​mName”, “win10”);
Get methods in Selenium WebDriver driver = new Firefo​xDr​ive​r(c​aps); // Pass the capabi​lities
as an argument to the driver object
getT​itl​e() used to retrieve the current title of the webpage
getC​urr​ent​Url() used to retrieve the current URL of the webpage
getP​age​Sou​‐ used to retrieve the current page source of the
rce() webpage
getT​ext() used to retrieve the text of the specified web
element
getA​ttr​ibu​te() used to retrieve the value specified in the attribute

Navigation

Refresh Page driv​er.n​av​iga​te(​).r​efr​‐


esh()
Navigate forwards in the browser driv​er.n​av​iga​te(​).f​orw​‐
history ard()
Navigate backward in the browser driv​er.n​av​iga​te(​).b​ack()
history

By Narayanan Palani Published 15th November, 2016. Sponsored by CrosswordCheats.com


(Narayanan Palani) Last updated 23rd October, 2020. Learn to solve cryptic crosswords!
cheatography.com/narayanan- Page 5 of 5. http://crosswordcheats.com
palani/

You might also like