Understanding of ” findelement in Selenium Webdriver. “


Before starting this blog just read “Installation Of Selenium Web driver in Eclipse” to understand Selenium Installation.

For being an Automation engineering, the 1st and foremost important thing is to find element on the Web-Application, in which we most struggle.
So here we are to help you out in finding different element in a very easy and simple way,

” findElement(By by) ”   as the name itself suggest that we can find the element by different technique which Selenium have provided us

ID
Link Text
ClassName
CssSelector
Name
PartailLinkText
TagName
Xpath

for finding the ID , Tag Name or link text etc. we usually go through page-source which is the most painful task to find the specific code in the chuck of code under page source.
Don’t worry here we can find web element in the very easy and simple way,

As we know selenium support Firefox as a default browser do you know why?
Here is the answer for that –>
Here are quite few important Add-on which FireFox provide us to make our life simple and easy,
1) Fire-Bug.
2) Firefinder extension of Firebug.
3) FirePath extension of Firebug.

Firebug : Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse.
You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

Firefinder for Firebug : Finds HTML elements matching chosen CSS selector(s) or XPath expression

FirePath for Firebug : FirePath is a Firebug extension that adds a development tool to edit, inspect and generate XPath 1.0 expressions, CSS 3 selectors and JQuery
selectors (Sizzle selector engine).

Let have a over  look one by one with example which will make your idea much clear.

Before we go to find element we first have to take control over the browser,

                                                     public WebDriver driver;

                                                    driver = new FirefoxDriver();

Now , we have taken the control of FireFox browser in variable called driver.

Now we are all set for finding element by different elements,

webdriver_image

Example for Find Element by ID:

As shown in the above image we have text-box with ID as “id”

driver.findElement(By.id(“id”));

now we have control over the text-box with ID as “id” now we can preform multiple choice what action we have to perform on it.

( Note : As we have Text-Box as example we would like to enter text into it )

driver.findElement(By.id(“id”)).sendKeys(“Entered Text”);

Example for Find Element by LinkText:

driver.findElement(By.linkText(“LinkText”));

now we have control over link element now it’s time to perform multiple option on it.

( Note : As we have Link as example we would like to click into it )

driver.findElement(By.linkText(“LinkText”)).click();

Example for Find Element by ClassName:

driver.findElement(By.className(“ClassName”));

now we have control over Text-Box with ClassName as “ClassName” element now it’s time to perform multiple option on it.

( Note : As we have Text-Box as example we would like to enter text into it )

driver.findElement(By.className(“ClassName”)).sendKeys(“Entered Text”);

Example for Find Element by PartailLinkText:

driver.findElement(By.partialLinkText(“PartailLinkText”);

now we have control over link element now it’s time to perform multiple option on it.

( Note : As we have Link as example we would like to click into it )

driver.findElement(By.partialLinkText(“PartailLinkText”).click();

Example for Find Element by Name:

driver.findElement(By.name(“Name”));

now we have control over the text-box with name as “Name” now we can preform multiple choice what action we have to perform on it.

( Note : As we have Text-Box as example we would like to enter text into it )

driver.findElement(By.name(“Name”)).sendKeys(“Entered Text”);

Example for Find Element by CssSelector:

Till now we have seen number of example which was quite easy, but most of the time it’s quite possible you came across such example where all the above example get fail,
so here we can have another easy solution for you.

with the help of below example we would like you to go through by Drop-down box , how to take control of Drop-Down too.

Till now we have taken the control of browser but now we have to take control of the drop-down

Select Drop_Down ;

Drop_Down = new Select(driver.findElement(By.cssSelector(“Expression Obtained by findFinder”)));

(Note : How to obtained expression of Css by Firefinder –

Drop-Down

uniquePaste the “Copy Unique Selector” in the Text box of FireFinder as shown Below and click on the filter button to check weather the element is identified properly or not.

firefinder

now we have control over drop-down, now it’s time to perform multiple option on it.

( Note : As we have option as example we would like to click into it

Drop_Down.findElement(By.cssSelector(“[type=’button’][value=’->’]”)).click();

(Note :One Another way of finding CssSelector is to put the multiple attribute in a series)

Example for Find Element by Xpath:

Just by going to the console developer tool bar and with help of FirePath extension we can easily find the Xpath of the particular element.

driver.findElement(By.xpath(“xpathExpression”);

(Note : Aswe have radion button as a example , we would like to have Click operation on it.

driver.findElement(By.xpath(“xpathExpression”)).click();

By this last example we have come to the end of this post keep waiting for next informational Post.

Post By :

Ankit Khandelwal
QA, Software Tester at Webonise Lab Pvt Ltd
khandelwal.ankit89@gmail.com
in.linkedin.com/pub/ankit-khandelwal/32/477/a94/

 

Thanks to Ankit for writing post for my blog. Nice information shared with all of us.

Suggestions and Feedback are most welcome.