简体   繁体   中英

Selenium clicking on javascript link

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?

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM