📺 Develpreneur YouTube Episode

Video + transcript

Introduction To Selenium - Part 3

2021-11-23 •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]
all right so once we have our locator we
now need to use that locator to actually
retrieve that web element so we can
actually do something
so for that we need a web
element
class
and here we will call this item search
and we will say equal
now in order to find the web element you
need to use the web driver that we
instantiated so we want to actually now
connect to using the driver go to the
browser and we want to use the find
element method
to pass in our locator to actually go
look up that web element that's on the
page and once we
find the element we want to do something
so if we
if we look at the methods for web
element you can do things like clear an
input field you can click the field or
button or link
uh you can also find elements within the
element so if you
were dealing with like ordered lists or
linked
or
ordered lists or unordered lists you
could actually grab the list itself and
then
loop through
all the elements inside of the list if
you want to interact with each list item
other things you can do you can get the
size you can get the location you can
get the text if there's text you can get
the tag name and so on you can also
get the attribute so you can actually go
pull out the
specific attribute values
uh or attribute ids to get the values
that's associated with it so if
for example with the walmart
we have all these additional attributes
inside of our
tag here so we have the area
autocomplete so we can actually look for
these attributes and make sure that they
are there and make sure that they are
set to the correct value
okay
now for this particular case i want to
just
type in
the text playstation so in order to
simulate a user
typing you would use the method send key
and here
you can pass in a character sequence so
we will type in
play
station
now right now this will just put in the
text playstation now we could
look for the additional web element
of this little magnifier which is our
search button which if we inspect it
we see it is a
image wrapped with a link
inside of a button here so if we were to
hover over and click this we're actually
clicking the button which will do a
submit to our form
however
i don't want to look up another web
element here i just want to make sure
that i can type in playstation
and hit the enter key which should do
the search for playstation
so to simulate that over here in our
code we will do our item search again
for our web element
and we are going to send another set of
keys but this time we're going to use
the keys class
which comes in cylinder which gives you
access to all the additional keys that
are not standard text so you have the
plus you have the minus you have the alt
key the arrow keys you also have the
enter key
we will
simulate passing in enter
and then we will give
the page time to load
and then it should open up another page
with our search so we should see
something like this
so we should see a list of playstation
items pop up in our window here that's
what we should see
now over here on the left we also notice
that
when we started out we didn't have the
navigation on the left to filter out our
search criteria
so if we start here with playstation
then we get a filter list over here on
the left so what i want to do for my
next step is i want to look for this
link playstation
so if i in
if i just copy that just copy the text
we're going to look at the other two
methods that you can use
with by
so here this will be
filter
locator
and this will be by dot
link text or partial link text so we'll
start with length text and we'll
actually put in the full text
playstation is for console
but we could also
simplify this and actually just look for
ps4
now there's a catch to this if you use
the link text
or partial link text
you have to make sure that
this text
is only assigned to one link
if you have more than one link or more
than one location where this text is
attached to a link you're going to get
back more than one element
or locator when you actually look it up
okay
again we want to come back out here we
want to inspect this guy
to bring up our development console
make sure we do
our control f to bring up our search
and we will type in the text we're
looking for
all right so we see it's here it's in
one location here where it's text
oh it's in another location down here in
text so this
looking for this particular one might
not work
uh here it is inside of text here so
that guy should work because he is a
link
this one actually won't be a problem
because if you look at the div tag not a
link tag
first one as well was also a div not a
link
and this guy's just in some javascript
so we're okay there so we should be all
right with that
and what should happen is we should see
it click the playstation 4 and then we
should see the identification here the
filter on the left change
right so we have our search
and then we will
need another web element
and again we will use driver
find
element
we'll use our search filter this time
now this time because it's a link we
just want to click
we just want to actually click
our
webinar
now we just want to simulate a user
click
then again we'll add a little short
pause here
now if you actually do start creating uh
your own selenium test or automated
tests do not use thread dot sleep this
is just for presentation purposes only
you would want to actually look up how
to use the weights class
a book
we can cover that in another topic if
you guys find this interesting
all right so we'll run this guy so we
should see it type in playstation
it did we clicked enter
and we should see the filter over here
change and it did so so far we've
navigated through we've clicked a few
things
so now let's try one additional step
let's add uh an actual validation so
let's make sure that the first item in
our list here uh contains the sony
playstation 4 terabyte one slim gaming
so here we'll do
inspect
now this is where uh this is a good
example of where xpath is going to come
in handy or css selectors if you notice
this text is just inside of a span tag
here but it does not have any attributes
whatsoever no id no name no link so we
can't really use any of the simple
locator methods like name id
class name or even link text
we have to actually do xpath or css
selector
so here we want to probably go up and
find the very first place where we
actually have an id or some type of
identifier
now i'm going to show you a
cheat way to do this
so you can do ctrl f
or
you can right click
on the element
go back to inspect get to the actual
element so we're inside of here
actually i want the span because i want
the whole text
so we want this guy
you can right click in the element code
and copy now gives you the option to
copy your
css selector
full xpath or expat
so we will copy the x path and we will
paste it in here
now the reason i didn't just take this x
back and run is because as you see here
it's got a whole bunch of
pathways to get to our particular span
so we're doing div div div one two three
um
this could get tricky because if they
actually change
uh any of the paths information in here
you're gonna break your look up for this
test so my recommendation is actually to
get the search product results
which gives you this guy here
and then we want to walk down and
get the div here and actually i want the
span
all the spans
and
we have our search results
we have our list view
so this guy is really not that clean we
want to get down to this guy here
so we come down here project image span
our divs
still not quite where we want so if we
do inspect again
so what we could do
is we can look for the search product
title for list view here
so let's actually change this up so a
chance to do it by class
and look for this oh look now we
actually get the 20 items so we have 20
results returned to us so this is going
to give us our list of 20.
and now here
we could then walk into that list so we
can take the first one
and we can go okay
expand
so as you can see it's now returning me
all the spans
so it's going to return me all the
titles here
all right so let's take this one
and let's actually use the other
find method
so once we are on this page once we have
our results we can do another buy
locator
and this is going to be
search
results
equals by
dot xpath
now
since this
locator is going to return us multiple
elements we need to create a list of web
elements
this will be our locator
[Music]
again we do use the driver class but
instead of the find element method we're
going to use the find elements method
which returns us a list of web bills you
use this guy when you are going to be
retrieving more than one
and you want to make sure that you
import
the
java util list not the awt that one
won't work
and then we want to look for the text
sony playstation 4 one terabyte splint
or let's look for one
this text here
all right if i come back over here we're
going to be looking for the string
expected
text
okay and now
let's actually loop through
our search results and find the expected
text
now because text is actually text here
this is inside of our tag we can use the
get text
method to actually
get the text inside of our tag to do the
comparison so here we'll create a for
loop
web element
and we will do
search
result
and we'll do the search
results
so i'll loop through our list
and in here
if
our search
result dot get text
dot contains
our expected text
let's see
one additional condition
here so let's add a boolean flag
so we'll start with true
and so pass
before true
and we'll break it out
all right so if our search results dot
get text contains our text
set are going to true saying the test
pass and break out of our loop
if it does not find it
it will
result in false so we'll do a system out
and
test
results
and we'll write out what we expected
and run this guy
upper window we enter in our text we hit
enter we click the link on the left
and we now look through our results
and we see that it found the text
for one terabytes one gaming console
so this is
it in a nutshell as to how to use uh
locators
uh within selenium to look up different
uh
or in order to use different methods to
look up your locators in your browser
also with firefox chrome and edge now
you can also pull up the
uh developer console here and go to the
elements and each of these browsers
should have some type of
lookup locator down here that you can
use to look up your web elements within
your page
does anyone have any questions
okay
no questions
like to thank you guys for joining us
today we appreciate your time and if
you'd like to discuss any of these
topics further you can send us questions
comments or suggestions through info
developer.com
uh you can click or contact us on our
developer.com site
you can follow us on twitter at
developernerd or you can still find us
on facebook.com developer.org and we
also have a
youtube video link and we're also on
vimeo our goal is making every developer
better thank you and have a great day
[Music]
you
Transcript Segments
0.48

[Music]

26.8

all right so once we have our locator we

29.199

now need to use that locator to actually

32

retrieve that web element so we can

33.84

actually do something

35.44

so for that we need a web

37.84

element

38.96

class

40.32

and here we will call this item search

44

and we will say equal

45.92

now in order to find the web element you

49.2

need to use the web driver that we

51.68

instantiated so we want to actually now

54.239

connect to using the driver go to the

57.12

browser and we want to use the find

60.64

element method

63.6

to pass in our locator to actually go

66.24

look up that web element that's on the

70.84

page and once we

73.2

find the element we want to do something

76

so if we

80.56

if we look at the methods for web

82.08

element you can do things like clear an

84.24

input field you can click the field or

86.72

button or link

88.24

uh you can also find elements within the

90.88

element so if you

92.88

were dealing with like ordered lists or

95.6

linked

97.52

or

98.4

ordered lists or unordered lists you

101.28

could actually grab the list itself and

103.2

then

104.399

loop through

105.759

all the elements inside of the list if

108

you want to interact with each list item

111.04

other things you can do you can get the

112.96

size you can get the location you can

115.6

get the text if there's text you can get

117.439

the tag name and so on you can also

122.719

get the attribute so you can actually go

125.28

pull out the

127.04

specific attribute values

129.119

uh or attribute ids to get the values

132.319

that's associated with it so if

134.879

for example with the walmart

137.52

we have all these additional attributes

140.16

inside of our

142.56

tag here so we have the area

144.56

autocomplete so we can actually look for

146.72

these attributes and make sure that they

148.64

are there and make sure that they are

150.319

set to the correct value

153.44

okay

155.519

now for this particular case i want to

158

just

159.599

type in

160.959

the text playstation so in order to

163.599

simulate a user

165.28

typing you would use the method send key

169.44

and here

170.4

you can pass in a character sequence so

173.28

we will type in

175.04

play

177.76

station

181.36

now right now this will just put in the

184.239

text playstation now we could

187.76

look for the additional web element

191.12

of this little magnifier which is our

193.04

search button which if we inspect it

196.4

we see it is a

199.04

image wrapped with a link

201.92

inside of a button here so if we were to

205.599

hover over and click this we're actually

207.44

clicking the button which will do a

209.68

submit to our form

214

however

215.2

i don't want to look up another web

217.68

element here i just want to make sure

219.92

that i can type in playstation

222.799

and hit the enter key which should do

226.08

the search for playstation

228.08

so to simulate that over here in our

230.879

code we will do our item search again

234.48

for our web element

236.319

and we are going to send another set of

238.879

keys but this time we're going to use

242

the keys class

244.319

which comes in cylinder which gives you

246.64

access to all the additional keys that

249.76

are not standard text so you have the

252.4

plus you have the minus you have the alt

254.48

key the arrow keys you also have the

258.4

enter key

259.68

we will

260.72

simulate passing in enter

264.96

and then we will give

267.28

the page time to load

269.12

and then it should open up another page

271.84

with our search so we should see

273.6

something like this

275.6

so we should see a list of playstation

278.16

items pop up in our window here that's

280.96

what we should see

282.639

now over here on the left we also notice

285.759

that

286.8

when we started out we didn't have the

288.96

navigation on the left to filter out our

291.36

search criteria

292.88

so if we start here with playstation

295.36

then we get a filter list over here on

297.919

the left so what i want to do for my

300.08

next step is i want to look for this

303.039

link playstation

306.08

so if i in

307.36

if i just copy that just copy the text

312.8

we're going to look at the other two

314.72

methods that you can use

316.96

with by

318.08

so here this will be

320.72

filter

322.4

locator

325.6

and this will be by dot

328.72

link text or partial link text so we'll

332.16

start with length text and we'll

333.36

actually put in the full text

334.96

playstation is for console

338.72

but we could also

341.44

simplify this and actually just look for

346.479

ps4

348.639

now there's a catch to this if you use

351.919

the link text

354

or partial link text

360.8

you have to make sure that

362.96

this text

364.56

is only assigned to one link

367.68

if you have more than one link or more

371.039

than one location where this text is

373.039

attached to a link you're going to get

375.039

back more than one element

378.16

or locator when you actually look it up

381.039

okay

382.96

again we want to come back out here we

385.039

want to inspect this guy

387.68

to bring up our development console

390.24

make sure we do

392.56

our control f to bring up our search

395.12

and we will type in the text we're

397.52

looking for

398.72

all right so we see it's here it's in

400.56

one location here where it's text

405.199

oh it's in another location down here in

407.68

text so this

409.199

looking for this particular one might

410.96

not work

414.639

uh here it is inside of text here so

416.96

that guy should work because he is a

418.88

link

421.039

this one actually won't be a problem

422.8

because if you look at the div tag not a

425.28

link tag

427.12

first one as well was also a div not a

429.28

link

436.56

and this guy's just in some javascript

438.4

so we're okay there so we should be all

440.24

right with that

441.44

and what should happen is we should see

443.36

it click the playstation 4 and then we

446.16

should see the identification here the

448.24

filter on the left change

455.919

right so we have our search

459.36

and then we will

461.52

need another web element

468.96

and again we will use driver

472.479

find

480.24

element

482

we'll use our search filter this time

485.599

now this time because it's a link we

488.16

just want to click

491.36

we just want to actually click

494.319

our

495.68

webinar

496.72

now we just want to simulate a user

498.96

click

502.08

then again we'll add a little short

503.759

pause here

509.44

now if you actually do start creating uh

512.24

your own selenium test or automated

514.08

tests do not use thread dot sleep this

516.719

is just for presentation purposes only

520.399

you would want to actually look up how

522.64

to use the weights class

524.88

a book

525.839

we can cover that in another topic if

527.6

you guys find this interesting

529.92

all right so we'll run this guy so we

531.519

should see it type in playstation

536.32

it did we clicked enter

539.12

and we should see the filter over here

540.959

change and it did so so far we've

543.519

navigated through we've clicked a few

545.2

things

546.08

so now let's try one additional step

549.04

let's add uh an actual validation so

552.08

let's make sure that the first item in

554.24

our list here uh contains the sony

557.279

playstation 4 terabyte one slim gaming

560.8

so here we'll do

562.56

inspect

565.76

now this is where uh this is a good

568.48

example of where xpath is going to come

571.36

in handy or css selectors if you notice

574.64

this text is just inside of a span tag

576.959

here but it does not have any attributes

579.68

whatsoever no id no name no link so we

583.36

can't really use any of the simple

587.12

locator methods like name id

590.72

class name or even link text

594.16

we have to actually do xpath or css

596.72

selector

598.56

so here we want to probably go up and

601.279

find the very first place where we

604.24

actually have an id or some type of

607.68

identifier

609.2

now i'm going to show you a

611.2

cheat way to do this

613.76

so you can do ctrl f

615.68

or

616.56

you can right click

618.399

on the element

621.44

go back to inspect get to the actual

623.36

element so we're inside of here

626.24

actually i want the span because i want

628.64

the whole text

630.16

so we want this guy

631.76

you can right click in the element code

635.44

and copy now gives you the option to

638.56

copy your

639.839

css selector

641.44

full xpath or expat

644.48

so we will copy the x path and we will

647.12

paste it in here

648.56

now the reason i didn't just take this x

650.959

back and run is because as you see here

654

it's got a whole bunch of

655.76

pathways to get to our particular span

658.64

so we're doing div div div one two three

661.76

um

662.88

this could get tricky because if they

665.279

actually change

666.88

uh any of the paths information in here

670.079

you're gonna break your look up for this

672

test so my recommendation is actually to

674.88

get the search product results

680.64

which gives you this guy here

683.36

and then we want to walk down and

686.88

get the div here and actually i want the

690.399

span

695.2

all the spans

701.519

and

710.24

we have our search results

713.12

we have our list view

718.16

so this guy is really not that clean we

720.48

want to get down to this guy here

723.2

so we come down here project image span

727.12

our divs

729.04

still not quite where we want so if we

731.2

do inspect again

733.76

so what we could do

735.6

is we can look for the search product

738.72

title for list view here

742.399

so let's actually change this up so a

744.72

chance to do it by class

750.959

and look for this oh look now we

752.88

actually get the 20 items so we have 20

757.04

results returned to us so this is going

759.36

to give us our list of 20.

762.399

and now here

765.519

we could then walk into that list so we

768.16

can take the first one

769.92

and we can go okay

773.839

expand

775.68

so as you can see it's now returning me

778.16

all the spans

780.16

so it's going to return me all the

781.76

titles here

783.279

all right so let's take this one

787.36

and let's actually use the other

791.68

find method

792.959

so once we are on this page once we have

795.44

our results we can do another buy

797.839

locator

799.6

and this is going to be

801.68

search

803.76

results

807.12

equals by

809.2

dot xpath

816.959

now

818

since this

819.68

locator is going to return us multiple

822.48

elements we need to create a list of web

826.24

elements

830.16

this will be our locator

834.66

[Music]

852.24

again we do use the driver class but

854.72

instead of the find element method we're

857.519

going to use the find elements method

860

which returns us a list of web bills you

862.639

use this guy when you are going to be

864.24

retrieving more than one

869.76

and you want to make sure that you

871.36

import

874.959

the

875.839

java util list not the awt that one

879.519

won't work

881.279

and then we want to look for the text

888

sony playstation 4 one terabyte splint

892

or let's look for one

894.32

this text here

896.48

all right if i come back over here we're

899.199

going to be looking for the string

902.56

expected

904.88

text

910.839

okay and now

915.36

let's actually loop through

917.6

our search results and find the expected

920.32

text

921.44

now because text is actually text here

924.959

this is inside of our tag we can use the

928.24

get text

931.839

method to actually

933.6

get the text inside of our tag to do the

936.24

comparison so here we'll create a for

938.56

loop

944.24

web element

946.16

and we will do

948.56

search

950

result

952.639

and we'll do the search

958.16

results

960.88

so i'll loop through our list

962.959

and in here

964.16

if

966.839

our search

970.639

result dot get text

976.959

dot contains

980.959

our expected text

991.36

let's see

992.56

one additional condition

994.839

here so let's add a boolean flag

1003.04

so we'll start with true

1005.759

and so pass

1008.16

before true

1010.16

and we'll break it out

1021.199

all right so if our search results dot

1023.92

get text contains our text

1027.919

set are going to true saying the test

1030.959

pass and break out of our loop

1033.12

if it does not find it

1035.36

it will

1036.88

result in false so we'll do a system out

1041.52

and

1044.48

test

1045.439

results

1048.64

and we'll write out what we expected

1053.28

and run this guy

1060.48

upper window we enter in our text we hit

1063.36

enter we click the link on the left

1067.52

and we now look through our results

1070.24

and we see that it found the text

1073.44

for one terabytes one gaming console

1077.28

so this is

1078.64

it in a nutshell as to how to use uh

1081.6

locators

1082.799

uh within selenium to look up different

1085.919

uh

1086.64

or in order to use different methods to

1089.039

look up your locators in your browser

1092.16

also with firefox chrome and edge now

1095.52

you can also pull up the

1097.44

uh developer console here and go to the

1100.08

elements and each of these browsers

1102.559

should have some type of

1104.48

lookup locator down here that you can

1106.24

use to look up your web elements within

1109.12

your page

1111.12

does anyone have any questions

1116

okay

1117.44

no questions

1120.64

like to thank you guys for joining us

1122.72

today we appreciate your time and if

1124.799

you'd like to discuss any of these

1126.16

topics further you can send us questions

1128.559

comments or suggestions through info

1131.72

developer.com

1133.28

uh you can click or contact us on our

1136.6

developer.com site

1138.48

you can follow us on twitter at

1139.84

developernerd or you can still find us

1141.84

on facebook.com developer.org and we

1144.88

also have a

1146.64

youtube video link and we're also on

1149.039

vimeo our goal is making every developer

1151.52

better thank you and have a great day

1156.43

[Music]

1171.039

you