如何在 Selenium RC 中使用按钮的 Xpath? [英] How to use Xpath of button in Selenium RC?

查看:24
本文介绍了如何在 Selenium RC 中使用按钮的 Xpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Selenium RC 工作.任何人都可以让我知道如何在 Selenium (Java) 中为按钮编写 xpath 吗?

I am working in Selenium RC. Can anyone please let me know how to write xpath for button in Selenium (Java)?

推荐答案

您应该在 Selenium IDE 中开发脚本 (download) 在将其移植到 Selenium RC 之前.在 Selenium IDE 中,当您单击网页上的任何内容时,它应该会自动为您单击的元素生成某种选择器.然后,一旦您记录了所有事件,您就可以使用您使用的任何语言对其进行格式化,然后将其复制并粘贴到您的 Selenium RC 代码中.

You should develop the script in the Selenium IDE (download) before porting it to Selenium RC. In Selenium IDE, when you click anything on the webpage, it should automatically generate some kind of selector for the element you clicked. Then, once you've recorded all the events, you Format it in whatever language you're using, and then you copy and paste it to your Selenium RC code.

但是 Recorder Javascript 不是万无一失的(例如,如果您单击 divXMLHttpRequestsetTimeout,不会被记录).或者,可能会记录点击,但您可能不喜欢 Selenium 为元素选择的选择器.无论哪种情况,您都必须根据 DOM 结构编写自己的选择器.要查看 DOM 结构,如果您使用的是 Firefox,请打开 Firebug (F12),如果您使用的是 Chrome,请打开检查器 (Ctrl-Shift-J) 幸运的是,Selenium 理解 一堆选择器语法,所以如果你不知道 XPath,你可以使用 CSS 选择器.

But the Recorder Javascript isn't foolproof (e.g. if you click on a div that causes some XMLHttpRequest or setTimeout, it won't be recorded). Or, the click may be recorded but you may not like the selector that Selenium chooses for the element. In either case, you'll have to write your own selector based on the DOM structure. To see the DOM structure, open Firebug if you're in Firefox (F12), or open the Inspector if you're on Chrome (Ctrl-Shift-J) Fortunately, Selenium understands a bunch of selector syntaxes, so you can use CSS selectors if you don't know XPath.

如果您决定使用 XPath,则必须先学习它.我还没有找到任何好的教程(而且我不是 w3schools 的粉丝).但是可以随意使用我编写的 bookmarklet 来测试 XPaths.你可能会得到类似 //button[.="text on button"]//input[@value="text on button"] 的结果.

If you do decide to use XPath, you'll have to learn it first. I haven't found any good tutorials (and I'm not a fan of w3schools). But feel free to use a bookmarklet to test XPaths that I wrote. You'll probably end up with something like //button[.="text on button"], or //input[@value="text on button"].

这篇关于如何在 Selenium RC 中使用按钮的 Xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆