使用 Capybara Selenium 实现 Rails 自动化 - xpath
我正在 capybara 和 selenium 的帮助下从事 Rails 功能测试自动化工作。我们需要在这里广泛使用 xpath,而我对 xpath 非常陌生。
- cpaybara使用的xpath和一般的xpath不一样吗?
- 我们如何使用 xpath 调试错误,是否有像 firebug 这样的工具来确保 xpath 正确?(我知道我们可以使用“复制 xpath”链接从 firebug 获取 xpath,但这似乎不起作用) 。
现在我遇到了以下错误,
Failure/Error: find(:xpath, '//div[@class="comment"]/div/div/div/ul/li/a[@title="Delete"]').click
非常感谢任何帮助...
编辑: 编辑了上面代码中的语法错误。
I am working on rails functional testing automation with the help of capybara and selenium. We need to make use of xpath widely here and I am very new with xpath.
- Is the xpath used by cpaybara is different from the general xpath?
- How can we debug the error with xpath, is there any tool like firebug to make sure that the xpath is correct?( I know we can get the xpath from firebug using 'copy xpath' link, but that does not seems to be working).
Right now am stuck with the following error,
Failure/Error: find(:xpath, '//div[@class="comment"]/div/div/div/ul/li/a[@title="Delete"]').click
Any help is highly appreciated...
Edit:
Edited the syntax error from the above code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在最后一个锚点中使用 contains 函数作为替代方案
要验证 XPath,请检查 XPath Visualizer
http://www.huttar.net/dimitre/XPV/TopXML-XPV.html
此问题的更多详细信息
You can try a contains function within the last anchor as an alternative
To verify XPath checkout the XPath Visualizer
http://www.huttar.net/dimitre/XPV/TopXML-XPV.html
More details at this SO question
cpaybara使用的xpath和一般的xpath不一样吗?
答案: xpath 会很常见,但并非总是如此,因为某些框架不支持 xpath 方法,例如
要使用 textnode 定位文本,我有下面的示例 xpath
但是selenium不支持使用textnode来定位元素,所以上面的xpath不起作用。
如何使用xpath调试错误,有没有像firebug这样的工具来确保xpath正确?
答案:在 Chrome 浏览器 v70 中,使用以下步骤评估和验证 xpath:
Element
选项卡查找自动生成的 xpath
右键单击您想要的元素>检查
它将突出显示相应的元素,选择它并右键单击。它显示以下选项:
选择复制的xpath,您将得到自动生成的xpath,您可以按照上面提到的相同方式进行评估以确保其正确
Is the xpath used by cpaybara is different from the general xpath ?
Answer: xpath will be common but its not all time true because some framework doesn't support the xpath methods e.g.
To locate a text using textnode i have below sample xpath
But selenium doesn't support to locate an element using textnode so above xpath won't work.
How can we debug the error with xpath, is there any tool like firebug to make sure that the xpath is correct?
Answer: In Chrome Browser v70 use below steps to evaluate and verify xpath:
Element
tab selected in consoleTo find auto generated xpath
Right click on your intended element > inspect
It will highlight the corresponding element, select it and do right click. It show below option :
select the copy xpath, you will get the auto-generated xpath, you can evaluate the same as mention above to make sure its correct