java Selenium 点击 javascript 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4795191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:03:42  来源:igfitidea点击:

Selenium clicking on javascript link

javaselenium

提问by krazycat

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

我在使用 Java 和 Spring 编写 Selenium 测试时遇到问题。

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 在包含多个删除按钮(文件列表)的页面上单击删除按钮。使用 Selenium IDE 生成以下代码

selenium.click("link=Delete");

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?

我需要a)找到一种方法来返回正确删除操作的xpath,或者b)通过java代码发送javascript命令本身。我也无法弄清楚如何去做,有人能指出我正确的方向吗?

采纳答案by Jonny Buchanan

Is the "Filename" part unique?

“文件名”部分是唯一的吗?

If so, the appropriate XPath would be:

如果是这样,适当的 XPath 将是:

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.

另外,您是否单击了 Selenium IDE 中的第一个删除链接?如果是这样,请尝试单击随后的其中一个,看看它会出现什么。

回答by Mopper

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

如果它是一种找出您想要的按钮的 xpath 的工具,那么有多种选择:

  • 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
  • 使用FireBug(Firefox 插件):如果在 firebug 的 HTML 选项卡中右键单击某个元素,则可以将其 Xpath 表达式复制到剪贴板。
  • 使用Xpath(Firefox 插件):一个允许您生成、编辑和检查 xpath 表达式的插件

回答by rs79

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

我偏爱使用 CSS 作为 Selenium 标识符有两个原因 -

  1. Readability
  2. Browser Independence (XPath recognition is slower on IE)
  1. 可读性
  2. 浏览器独立性(IE 上的 XPath 识别速度较慢)

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:

在您的源代码中,带有 CSS 标识符的 Selenium 命令将显示为:

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

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

如果 href 属性确实提供了其他删除按钮之间的区别

回答by Mario F

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.

有一个名为XPath Checker 的firefox 扩展。您可以在 firefox 中打开 html 源代码,然后获取所需的 XPath。但是,我记得一些 XPath 表达式在 Selenium 中无法正常工作时遇到了问题。