aspose file tools*
The moose likes Testing and the fly likes Selenium Web driver, finding XPATH expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Selenium Web driver, finding XPATH expression" Watch "Selenium Web driver, finding XPATH expression" New topic
Author

Selenium Web driver, finding XPATH expression

sugan san
Ranch Hand

Joined: May 23, 2013
Posts: 47
Hi I need to write XPATH expression for finding input projectName from the below XML and have to enter/sendkeys with that using selenium webdriver. Anyone can help me building xpath expression?


<div class="container-fluid container-fluid-my">
<div class="row-fluid">
<div class="row-fluid" style="margin-bottom: -22px; margin-top: 5px;">
<div class="row-fluid">
<div class="span12">
<div class="info-container">
<ul>
<li class="labelArrow">Project Name</li>
<li class="pointer"/>
<li>
<input id="projectName" class="projectmetadata" type="text" placeholder="Add Project Name" name="projectName" value=""/>
</li>
</ul>
</div>

I already tried many options like
driver.findElement(By.xpath("//input[@id='projectName']//input[@type='text']")).sendKeys("test");

But its not working.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

This means find input tags with an id of project name (so far so good) and then look for child input tags with type text. This isn't what you want because an input tag can't have another one as nested element.
//input[@id='projectName']//input[@type='text']

Is there any reason you can't just do //input[@id='projectName']? Ids in HTML are supposed to be unique.

If the HTML is poorly formed and has duplicates, try //input[@id='projectName'][@type='text']


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Doug Slavens
Greenhorn

Joined: Sep 10, 2013
Posts: 11
Have you tried creating an html file from the xml, opening it in firefox, and recording the input using the Selenium IDE plugin? Once it is recorded, copy and paste the input line from the IDE as Java into your code. I do agree, this line:

driver.findElement(By.xpath("//input[@id='projectName']")).sendKeys("test");

or the one mentioned earlier should work.
Roland Schmit
Greenhorn

Joined: Oct 17, 2013
Posts: 8
Hi,

the Firefox Add on Firebug is a great tool to get xpath or css expressions.

Roland
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selenium Web driver, finding XPATH expression