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(By.name("q"));

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

we.sendKeys("selenium:);

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.

https://blogs.oracle.com/rajeshthekkadath/entry/xpath_searching_for_a_text


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.

i. http://www.w3schools.com/xsl/xpath_intro.asp
ii. http://www.tutorialspoint.com/xpath/
iii. https://www.seleniumeasy.com/selenium-tutorials/xpath-tutorial-for-selenium

How to use xpath in scrip


Sample code

WebDriver driver = new FirefoxDriver();

driver.findElement(By.xpath("//*[@id='BLUE_BAR_ID_DO_NOT_USE']/div/div/div[1]/div/div/ul/li[12]/a/span/span")).click();




Share this

Related Posts

Previous
Next Post »