📺 Develpreneur YouTube Episode

Video + transcript

Introduction To Selenium - Part 2

2021-11-18 •Youtube

Detailed Notes

Selenium is a popular way to automate browser activity, record sessions, and setup testing scripts. In this series of tutorials we look at using Java and Selenium for all your browser automation needs.

Transcript Text
[Music]
now if you
don't just want to use gif but you
actually want to navigate you can also
do driver
dot
dot navigate
and the navigate method with our web
driver gives us the ability to use the
navigation fields of our browser so we
can use back and forth
so here we can do navigate to
we can go back
we can go forward
let's see forward back we can navigate
two
so you have the three different options
so you can actually do what get does by
doing two
if you want to use the history you can
go back
or forward
that's just another way to navigate
all right now once we have the page open
we actually want to do something with it
let's go back over here and open up a
browser go back to walmart
so say we wanted to test our little
input box here our search box and we
wanted to
test for playstation
so if i wanted to actually type in oops
i didn't need to click that okay so if
we wanted to simulate a click inside of
this input field and simulate a person
typing in playstation
and then simulating an enter key
we would have to do we'd have to locate
this web element first so in your
browsers
depending upon if you're in chrome or
firefox
how you look up your window is slightly
different
but if you go to
below this every time they come out with
a new update things move around there we
are so if you're on mac you go to view
developer you want to view source
now if you're in windows uh the f12 key
typically will open up the window for
you
all right walmart
tom
now we'll go and inspect
there we go so this brings up the
development console and in here you can
inspect the individual uh
fields that you want to interact with
you can also click this little
arrow key icon in the development tools
here if you click that it turns blue and
then you can hover over
your page and you'll see all the
different web elements get highlighted
so we'll go to this guy here and once
you left click it will take you to the
individual
element that you wanted to go to
let me hide this piece here we don't
care about that right now so in the
elements tab here this contains all the
source code
that's on the page but it allows you to
navigate to a certain individual element
and so here we have an html element
input is of type text so it's an input
field now it's got a whole bunch of
gallery goop here so we have css
information here for class it's got some
additional
labeling behind the scenes here possibly
for some bootstrap or some
type of
display tool or
language that they're using for the page
we have auto cap auto complete so this
could even possibly be jquery or a
bootstrap field
uh let's see we have autocorrect
placeholder ah here's name so we have a
name identifier so this is a form field
so when we click submit or click the
search field it will pass the name
or pass the text in for the name
query so it would be query value or name
value
uh do we have an id we do not
so there's different things here that we
could search for if i copy this
[Music]
element
and go into our code
so if we wanted to interact with this
field
we have a couple different options so we
would use the by class
and we will call this
this is
search
locator
i like naming things very descriptive um
basically
comment by name
is my
typical
means of coding these days
so we'll do
by locator so we'll do buy
and we are going to look it up by a
couple different ways so here we can do
by class names
if we did by class name we would have to
check out which one of these class names
we could use
so if we actually copy that if we go
back over to our browser
inside of our elements tab here if we
actually do control f we bring up
a
little search bar here where we can
search by string we can use css selector
text or we can use the xpath code to
actually
configure or look up our
so we can start by pasting in the class
information you see here we get 101
which is what we want to look for
uh let's see we have class h underscore
a
a a b so we have a couple of different
style sheets in here but let's just
start with the first one
and kind of whittle our way through it
to see if we can determine if we can get
one of one with that yeah that one
doesn't work let's try the next one
that one did work okay so if we use
a underscore b
that will give us this one element so we
can identify it this way so if we go
back to our code we can look it up like
this
so this guy will return us one web
element
so let's see if we don't need class
anymore we'll take that out
let's see we have name
of query
so we can
look for a name
but just to be a hundred percent sure
let's come back in here and type in
query
now query is going to give us a bunch of
different things but what we're looking
for is we're looking to make sure that
query is only applied to one name or id
so we got name
jquery dick query query selector
yeah okay queries unique
to our input field here
go back over
so the next way we can
look up
is doing
so we can take this again
but instead of class name
do by
name
here
let's see we can do a couple other ways
as well so if we go back over to our
code or to our browser
okay let's see we have query
we could oh there is an id so we have an
id here global search input if we copy
that
you should only have one now this is
also a good way to test if your page is
broken because you're not supposed to
have multiple ids on a page
it should always be unique so this is a
good way to test for that
okay so it came back as unique so we can
use that one so we could also
search for this one
and this time we'll do by id
now say we didn't have a name field we
didn't have
so if we didn't have a name we didn't
have a link we didn't have text
we could actually look up by uh xpath or
css selector
okay
this way so if i do dot
we can do by css selector
or
we could do
xpath
now these are the two most powerful ways
you can actually find web elements on a
page and when you're dealing with more
abstract or more complicated websites
especially those that do not
follow typical coding standards or
naming conventions
in part if you're using like bootstrap
or a lot of javascript libraries that
are kind of custom built where you don't
have control over the id
of the labels or the fields then you
need to kind of get a little um
creative as to how you actually go about
finding your input fields that are on
your page and to do that you can use
xpath which starts out with either the
absolute or relative path so if you want
to actually walk through your whole page
from the top you would start with the
relative path of forward slash which
starts you at the html
now you could do it under the head
and then you could go down into the
style
so you just kind of walk the path we're
walking the path from the top
which is great but yet we want to deal
with this field and it may be way down
in the middle of our code well for here
what we can do is we can actually use
the relative path so we could do slash
input
well that gives us all our input tags
but that's still not going to give us
the one
that we want it's giving us all seven
input fields that are on the page now we
can click through till we find the one
we want which is this guy
and then we have to actually add some
additional filter criteria so if you
want to actually pull from any of the
attributes that are in this tag you
would use the
at
identifier and then you could put in
like class you could put in id you could
even put in a placeholder so here we did
placeholder
and we want placeholder equal to search
walmart.com
and as you can see we have one one so we
could use this
to pull in our identifier web element as
well so this would go inside of the xcap
now csf is slightly different than xpath
and it's
it's cleaner
but it can also get ugly as well
so what's nice about css
is say we want to look up by class
instead of us having to actually um use
the class here
and instead of being able to do equal
you could shortcut
here so we want a 8b
if you see here this does not return us
this tag because this lookup is looking
for the explicit text that is inside of
class
now if we're doing it as a css selector
we could simply do our tag
input
and we can do dot telling hey we want to
look for a class
and then all we have to do is just put
in the text
so this is a very cleaner way of looking
up our
information so we could do it by that we
could do it by id
so if you use pounds you're saying hey i
want by id
and in our case here the id is global
search input
so
that's another way to do it now if you
want to actually navigate or walk into
the path with css selectors instead of
relative
and absolute you use greater than and
from there you can walk into the
nested tags underneath that
here this guy doesn't have one so let's
go up to this guy here
and let's start with him
so we now have this uh
the update
so we do do
so here's our tag we have our id but we
want to walk into this guy to actually
get
our input field
which is inside of oh he's inside of
this form
so we'll take the form
here we go all right so we have the form
and if you follow the line underneath
forum that basically tells you that all
of the
uh
tags here on the right are all nested
inside of the form tag here
so we want to walk into our path here so
if we do form greater than and we do
input
we're going to get two input fields so
the first one is going to be the global
search icon or cat id
and then the second one is the one we
want so we actually want this guy here
but what you can do is you can then walk
the path and then get the specific
child inside of this
input here
all right so
we'll do it the other way we'll just
keep it simple
we will do the
id let's go back over
so what we can do here
is we can do
an input
pound
here our input pound global search input
now you don't necessarily need to do
this for every for
one web element lookup i'm just
walking through the different ways you
can actually pull these in so here's an
example using
the style class
name id css selector or expat
any questions on these
you
Transcript Segments
0.48

[Music]

26.32

now if you

27.519

don't just want to use gif but you

29.119

actually want to navigate you can also

31.84

do driver

33.76

dot

36.16

dot navigate

38.96

and the navigate method with our web

41.44

driver gives us the ability to use the

44.079

navigation fields of our browser so we

46.079

can use back and forth

48.879

so here we can do navigate to

52.239

we can go back

54

we can go forward

57.12

let's see forward back we can navigate

59.52

two

61.44

so you have the three different options

63.44

so you can actually do what get does by

65.6

doing two

66.96

if you want to use the history you can

68.799

go back

70.24

or forward

73.76

that's just another way to navigate

79.439

all right now once we have the page open

81.84

we actually want to do something with it

83.759

let's go back over here and open up a

86.08

browser go back to walmart

90.4

so say we wanted to test our little

92.72

input box here our search box and we

95.36

wanted to

96.479

test for playstation

98.479

so if i wanted to actually type in oops

100.96

i didn't need to click that okay so if

103.2

we wanted to simulate a click inside of

105.28

this input field and simulate a person

107.84

typing in playstation

111.68

and then simulating an enter key

114.96

we would have to do we'd have to locate

117.759

this web element first so in your

121.28

browsers

122.479

depending upon if you're in chrome or

124.24

firefox

125.92

how you look up your window is slightly

128.479

different

130.16

but if you go to

133.76

below this every time they come out with

135.12

a new update things move around there we

136.879

are so if you're on mac you go to view

139.599

developer you want to view source

142.48

now if you're in windows uh the f12 key

145.76

typically will open up the window for

148.319

you

150.959

all right walmart

153.76

tom

156.48

now we'll go and inspect

159.12

there we go so this brings up the

161.04

development console and in here you can

165.12

inspect the individual uh

167.599

fields that you want to interact with

169.84

you can also click this little

172.239

arrow key icon in the development tools

175.519

here if you click that it turns blue and

178

then you can hover over

180.319

your page and you'll see all the

182.08

different web elements get highlighted

184.879

so we'll go to this guy here and once

186.8

you left click it will take you to the

189.28

individual

190.64

element that you wanted to go to

193.599

let me hide this piece here we don't

195.92

care about that right now so in the

198.64

elements tab here this contains all the

201.44

source code

202.72

that's on the page but it allows you to

204.959

navigate to a certain individual element

208.72

and so here we have an html element

211.44

input is of type text so it's an input

214.08

field now it's got a whole bunch of

216

gallery goop here so we have css

218.56

information here for class it's got some

221.36

additional

222.56

labeling behind the scenes here possibly

224.72

for some bootstrap or some

227.92

type of

229.68

display tool or

232

language that they're using for the page

234.239

we have auto cap auto complete so this

236.48

could even possibly be jquery or a

238.72

bootstrap field

240.48

uh let's see we have autocorrect

242.08

placeholder ah here's name so we have a

244.4

name identifier so this is a form field

247.2

so when we click submit or click the

249.68

search field it will pass the name

252.159

or pass the text in for the name

254.4

query so it would be query value or name

257.84

value

258.88

uh do we have an id we do not

262.24

so there's different things here that we

264.32

could search for if i copy this

268.15

[Music]

270

element

271.12

and go into our code

274.8

so if we wanted to interact with this

277.199

field

280.56

we have a couple different options so we

283.84

would use the by class

286.96

and we will call this

289.52

this is

292.16

search

293.52

locator

295.52

i like naming things very descriptive um

299.44

basically

300.56

comment by name

302.4

is my

303.84

typical

305.039

means of coding these days

307.68

so we'll do

309.36

by locator so we'll do buy

311.759

and we are going to look it up by a

314.639

couple different ways so here we can do

316.24

by class names

318.56

if we did by class name we would have to

321.28

check out which one of these class names

324.32

we could use

326.56

so if we actually copy that if we go

328.639

back over to our browser

333.039

inside of our elements tab here if we

335.44

actually do control f we bring up

338.96

a

339.919

little search bar here where we can

341.6

search by string we can use css selector

345.12

text or we can use the xpath code to

348.16

actually

349.12

configure or look up our

351.84

so we can start by pasting in the class

354.08

information you see here we get 101

356.4

which is what we want to look for

358.72

uh let's see we have class h underscore

360.8

a

362

a a b so we have a couple of different

364.639

style sheets in here but let's just

366.88

start with the first one

368.72

and kind of whittle our way through it

371.039

to see if we can determine if we can get

373.68

one of one with that yeah that one

376

doesn't work let's try the next one

383.84

that one did work okay so if we use

386.56

a underscore b

389.199

that will give us this one element so we

391.84

can identify it this way so if we go

394.319

back to our code we can look it up like

396.8

this

400.16

so this guy will return us one web

402.479

element

404

so let's see if we don't need class

405.44

anymore we'll take that out

412.479

let's see we have name

414.319

of query

415.68

so we can

416.88

look for a name

418.8

but just to be a hundred percent sure

421.28

let's come back in here and type in

423.44

query

426.08

now query is going to give us a bunch of

427.68

different things but what we're looking

429.199

for is we're looking to make sure that

431.039

query is only applied to one name or id

435.36

so we got name

437.52

jquery dick query query selector

441.759

yeah okay queries unique

444

to our input field here

446.319

go back over

448.8

so the next way we can

454.4

look up

455.68

is doing

457.039

so we can take this again

460.96

but instead of class name

464.879

do by

466.72

name

476.8

here

478.879

let's see we can do a couple other ways

480.96

as well so if we go back over to our

483.84

code or to our browser

487.039

okay let's see we have query

489.68

we could oh there is an id so we have an

491.919

id here global search input if we copy

494.72

that

495.68

you should only have one now this is

498.16

also a good way to test if your page is

500.16

broken because you're not supposed to

501.84

have multiple ids on a page

504.319

it should always be unique so this is a

506.16

good way to test for that

508.639

okay so it came back as unique so we can

511.36

use that one so we could also

515.44

search for this one

522.159

and this time we'll do by id

526.88

now say we didn't have a name field we

529.92

didn't have

534.48

so if we didn't have a name we didn't

536.16

have a link we didn't have text

538.72

we could actually look up by uh xpath or

542.08

css selector

543.68

okay

544.72

this way so if i do dot

547.04

we can do by css selector

549.839

or

551.68

we could do

554.72

xpath

557.36

now these are the two most powerful ways

559.839

you can actually find web elements on a

561.92

page and when you're dealing with more

564.32

abstract or more complicated websites

568

especially those that do not

570.48

follow typical coding standards or

573.36

naming conventions

575.36

in part if you're using like bootstrap

577.44

or a lot of javascript libraries that

580.08

are kind of custom built where you don't

582

have control over the id

584.24

of the labels or the fields then you

587.2

need to kind of get a little um

590.56

creative as to how you actually go about

592.56

finding your input fields that are on

594.56

your page and to do that you can use

597.519

xpath which starts out with either the

600.8

absolute or relative path so if you want

603.44

to actually walk through your whole page

605.04

from the top you would start with the

607.04

relative path of forward slash which

609.519

starts you at the html

617.6

now you could do it under the head

620.079

and then you could go down into the

621.68

style

622.88

so you just kind of walk the path we're

624.88

walking the path from the top

626.8

which is great but yet we want to deal

629.2

with this field and it may be way down

632

in the middle of our code well for here

635.04

what we can do is we can actually use

636.8

the relative path so we could do slash

639.36

input

642.24

well that gives us all our input tags

644.16

but that's still not going to give us

645.6

the one

646.56

that we want it's giving us all seven

649.04

input fields that are on the page now we

651.519

can click through till we find the one

653.44

we want which is this guy

655.92

and then we have to actually add some

657.839

additional filter criteria so if you

660.24

want to actually pull from any of the

662.24

attributes that are in this tag you

664.56

would use the

665.6

at

667.519

identifier and then you could put in

670.16

like class you could put in id you could

673.36

even put in a placeholder so here we did

676.959

placeholder

679.68

and we want placeholder equal to search

682.64

walmart.com

692.88

and as you can see we have one one so we

695.279

could use this

696.88

to pull in our identifier web element as

700.24

well so this would go inside of the xcap

705.6

now csf is slightly different than xpath

708.64

and it's

710.48

it's cleaner

712

but it can also get ugly as well

714.88

so what's nice about css

718.16

is say we want to look up by class

720.959

instead of us having to actually um use

723.519

the class here

726.24

and instead of being able to do equal

729.04

you could shortcut

730.48

here so we want a 8b

736.88

if you see here this does not return us

739.839

this tag because this lookup is looking

742.8

for the explicit text that is inside of

745.44

class

746.56

now if we're doing it as a css selector

749.12

we could simply do our tag

751.839

input

752.88

and we can do dot telling hey we want to

755.36

look for a class

757.6

and then all we have to do is just put

759.6

in the text

760.959

so this is a very cleaner way of looking

763.68

up our

766.24

information so we could do it by that we

768.24

could do it by id

769.92

so if you use pounds you're saying hey i

772

want by id

773.44

and in our case here the id is global

776.639

search input

778.72

so

784.24

that's another way to do it now if you

786.399

want to actually navigate or walk into

789.04

the path with css selectors instead of

792.399

relative

794

and absolute you use greater than and

797.92

from there you can walk into the

800.8

nested tags underneath that

803.12

here this guy doesn't have one so let's

805.279

go up to this guy here

808.959

and let's start with him

812.56

so we now have this uh

815.519

the update

818.72

so we do do

820.959

so here's our tag we have our id but we

823.839

want to walk into this guy to actually

826.8

get

827.76

our input field

832.88

which is inside of oh he's inside of

836

this form

836.959

so we'll take the form

844.88

here we go all right so we have the form

847.36

and if you follow the line underneath

849.44

forum that basically tells you that all

851.279

of the

852.399

uh

853.519

tags here on the right are all nested

856.16

inside of the form tag here

858.399

so we want to walk into our path here so

861.279

if we do form greater than and we do

864.48

input

865.519

we're going to get two input fields so

868.48

the first one is going to be the global

870.639

search icon or cat id

874.399

and then the second one is the one we

876.399

want so we actually want this guy here

881.199

but what you can do is you can then walk

882.88

the path and then get the specific

886.24

child inside of this

888.639

input here

890.72

all right so

892.079

we'll do it the other way we'll just

893.76

keep it simple

895.12

we will do the

896.839

id let's go back over

900.8

so what we can do here

902.88

is we can do

904.8

an input

906.48

pound

909.68

here our input pound global search input

917.519

now you don't necessarily need to do

919.279

this for every for

921.92

one web element lookup i'm just

924.72

walking through the different ways you

926.16

can actually pull these in so here's an

928.8

example using

930.32

the style class

932.48

name id css selector or expat

937.68

any questions on these

955.36

you