3

I'm having an issue with writing a Selenium test using Java and Spring.

I need Selenium to click on a delete button on a page that contains multiple delete buttons (a list of files). Using the Selenium IDE generates the following code

selenium.click("link=Delete");

which is basically useless. I haven't been able to figure out how to target the specific element contained in a table. Here's the source:

<tr onmouseover="mouseOver(this)" onmouseout="mouseOut(this)">
  <td class="thumbnail" align="center"><img src="/services/images/nav/resources.gif" /></td>
  <td colspan="3" onClick="nav('FileName'); return false">
    <a href="javascript:nav('FileName')">Basics</a></td>
 <td>
   <a class="actionButton" href="javascript:del('FileName')">Delete</a></td>
<td>&nbsp;</td>
</tr>   

I need to either a) find a way to return the xpath of the correct delete action or b) send the javascript command itself through the java code. I haven't been able to figure out how to to either, can anyone point me in the right direction?

2
  • Thanks everyone! I ended up with this xpath: selenium.click("//tr[td[a[contains(@href, '" + fileName + "')]]]/td/a[contains(text(), 'Delete')]"); and that worked like a charm! Commented Jan 27, 2011 at 15:05
  • If the below answers helped you, consider marking the most useful of them as answer. At least consider giving an upvote to all the answers which helped you. Commented Jan 31, 2011 at 12:02

4 Answers 4

3

Is the "Filename" part unique?

If so, the appropriate XPath would be:

selenium.click("//a[@href=\"javascript:del('FileName')\"")

Also, did you click the first Delete link from Selenium IDE? If so, try clicking on one of the subsequent ones instead and see what it comes up with.

Sign up to request clarification or add additional context in comments.

Comments

0

There is this firefox extension called XPath Checker. You can open the html source in firefox, and then get the XPath for what you want. However, I remember having problems with some XPath expressions that didn't work properly in Selenium.

Comments

0

If it's a tool to find out the xpath of your button you're after, then there are various options:

  • Use FireBug (Firefox addon): if you right-click an element in the HTML tab of firebug, you can copy its Xpath expression to the clipboard.
  • Use Xpath (Firefox addon): an addon which allows you to generate, edit and inspect xpath expressions

Comments

0

I am partial towards using CSS for Selenium identifiers for 2 reasons -

  1. Readability
  2. Browser Independence (XPath recognition is slower on IE)

That being said, the limitation is that multiple elements having the same name, should have a distinguishable attribute.

In your source, the Selenium command with a CSS identifier would read:

selenium.click("css=a.actionButton[href=javascript:del('FileName')]");

if indeed the href attribute provides distinction between the other Delete buttons

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.