如何在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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在 Selenium IDE 中开发脚本(下载),然后再将其移植到 Selenium RC。在 Selenium IDE 中,当您单击网页上的任何内容时,它应该自动为您单击的元素生成某种选择器。然后,一旦您记录了所有事件,您可以使用您使用的任何语言对其进行格式化,然后将其复制并粘贴到您的 Selenium RC 代码中。
但是 Recorder Javascript 并不是万无一失的(例如,如果您点击
div
会导致一些XMLHttpRequest
或setTimeout
,不会被记录)。或者,单击可能会被记录,但您可能不喜欢 Selenium 为元素选择的选择器。无论哪种情况,您都必须根据 DOM 结构编写自己的选择器。要查看 DOM 结构,如果您使用的是 Firefox,请打开 Firebug (F12);如果您使用的是 Chrome,请打开检查器 (Ctrl-Shift< /kbd>-J)幸运的是,Selenium 能够理解一堆选择器语法,因此如果您不了解 XPath,也可以使用 CSS 选择器。如果您决定使用 XPath,则必须首先学习它。我还没有找到任何好的教程(而且我不是 w3schools 的粉丝)。但请随意使用我编写的 书签来测试 XPath 。您最终可能会得到类似
//button[.="text on button"]
或//input[@value="text on button"]
。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.
But the Recorder Javascript isn't foolproof (e.g. if you click on a
div
that causes someXMLHttpRequest
orsetTimeout
, 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.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"]
.您可以使用
Firebug
找到按钮 Xpath,它是 Firefox 的附加组件,如上所述,Selenium IDE 也是另一个更容易找到的选项。You can find button Xpath by using
Firebug
which is an add-on for Firefox and as above answer Selenium IDE is also another, easier option to find.