• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selenium Web driver, finding XPATH expression

 
sugan san
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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']
 
Doug Slavens
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Roland
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic