简体   繁体   English

选择没有特定id,class,xpath等的元素

[英]Selecting an element with no particular id, class, xpath, etc

I am currently working on writing the test automation for a web application that loads as an SWF file for our end-users, but a fully functioning Javascript version exists for the sole purpose of automation. 我目前正在为Web应用程序编写测试自动化,该Web应用程序将作为SWF文件加载给最终用户,但是存在一个功能齐全的Javascript版本,其唯一目的就是实现自动化。

I have means to navigate through the application with keyboard shortcuts, but when it comes to executing click commands, I have no luck at all. 我可以用键盘快捷键在应用程序中导航,但是在执行点击命令时,我一点都不走运。 Upon inspecting with Firebug/Firepath, the only value that I could find was an xpath (no id exists, no class, no anything really). 使用Firebug / Firepath进行检查时,我能找到的唯一值是xpath(不存在ID,没有类,没有任何东西)。

The next issue is the xpath itself. 下一个问题是xpath本身。 It is: 它是:

  1. Really brittle. 真的很脆。

     .//*[@id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[3]/div[1]/img 
  2. This appears to be the xpath to the image that represents the button, not the button itself. 这似乎是表示按钮的图像的xpath,而不是按钮本身。

Executing .click() commands on the above type of xpath will do nothing until you manually hover your mouse over the button (regardless of moveToElement commands), where it will "click" the image but no functionality will run. 在手动将鼠标悬停在按钮上(无论moveToElement命令如何)之前,在上述类型的xpath上执行.click()命令将无济于事。在该按钮上,它将“单击”图像,但不会运行任何功能。

So I'm wondering after digging around in the actual JavaScript looking for identifiers, is there any way to select an element through any other properties? 因此,我想知道在实际的JavaScript中寻找标识符之后,是否可以通过任何其他属性来选择元素? Or is there any way I can better "identify" a function? 还是有什么方法可以更好地“识别”功能? Perhaps find the xpath to the button that the image represents? 也许找到图像表示的按钮的xpath?

Using JUnit and Java, if that helps. 如果有帮助,请使用JUnit和Java。

Thanks 谢谢

Apparently, my comments answered the OP's question, so here goes, for reference sake: 显然,我的评论回答了OP的问题,因此在此仅供参考:

If you need "the button itself" , as you wrote in your question, use Inspect Element from the browser to find out what the actual element is you need and then simply remove from the right-hand side of your XPath expression enough axis steps until the part that remains selects the ancestor element that is the actual button element. 如果您需要“按钮本身” (如您在问题中所写),请使用浏览器中的Inspect Element来查找所需的实际元素,然后只需从XPath表达式的右侧移除足够的轴步长,直到剩下的部分选择祖先元素,即实际的按钮元素。

Now you should be able to send it a click event. 现在,您应该可以向其发送click事件。

I'm afraid there won't be much we can do about the XPath statement to be "brittle", simply because you do not have an identifier to go on. 恐怕对于XPath语句“脆弱”,我们将无能为力,仅仅是因为您没有可继续使用的标识符。 That means that if the structure of the page changes, you will have to change the XPath (unless some of the ancestor elements have some notable identifiers). 这意味着,如果页面的结构发生更改,则必须更改XPath(除非某些祖先元素具有一些显着的标识符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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