How to Identify Web Elements Using Selenium Xpath and name

How to find element by Name in selenium

Elements do not necessarily have ID attributes on all of them. Elements can be have names that we can use to locate them uniquely. 

how to use chrome XPath isnpector
In the  image you will find value of the name attribute  of the google search text box is "q".
name is unique for this page.

Now we will write down java selenium code to uniquely identify the web element

WebDriver driver = new FirefoxDriver();
WebElement we = driver.findElement("q"));

Now we can write down the values in the search text box like below method.


How to find element by Xpath in selenium

Before start this topic we need to know what is xpath and how to build the xpath. Most of the time we are using xpath generator tool like Firebug , chrome developer tool ,but sometime we can not solely depend on those tool . We need to design xpath manually to overcome the complex situation.

If we look the the below link we will see there is a situation where we cant not use xapth generator tool directly because need to to use different function to handle the situation.

Please study xpath tutorial to learn what is xpath and how to build the xpath . I am providing few links which will give you a snapshot of the xpath.


How to use xpath in scrip

Sample code

WebDriver driver = new FirefoxDriver();


Share this

Related Posts

Next Post »

your ad