使用 Capybara Selenium 实现 Rails 自动化 - xpath

发布于 2024-12-12 02:06:21 字数 442 浏览 0 评论 0原文

我正在 capybara 和 selenium 的帮助下从事 Rails 功能测试自动化工作。我们需要在这里广泛使用 xpath,而我对 xpath 非常陌生。

  1. cpaybara使用的xpath和一般的xpath不一样吗?
  2. 我们如何使用 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.

  1. Is the xpath used by cpaybara is different from the general xpath?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深者入戏 2024-12-19 02:06:21

您可以尝试在最后一个锚点中使用 contains 函数作为替代方案

//div[@class="comment"]/div/div/div/ul/li/a[contains(text(), "Delete"]

要验证 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

//div[@class="comment"]/div/div/div/ul/li/a[contains(text(), "Delete"]

To verify XPath checkout the XPath Visualizer

http://www.huttar.net/dimitre/XPV/TopXML-XPV.html

More details at this SO question

浅浅 2024-12-19 02:06:21
  1. cpaybara使用的xpath和一般的xpath不一样吗?

    答案: xpath 会很常见,但并非总是如此,因为某些框架不支持 xpath 方法,例如

    要使用 textnode 定位文本,我有下面的示例 xpath

    //div[@id='some_id']/label/text()
    

    但是selenium不支持使用textnode来定位元素,所以上面的xpath不起作用。

  2. 如何使用xpath调试错误,有没有像firebug这样的工具来确保xpath正确?

    答案:在 Chrome 浏览器 v70 中,使用以下步骤评估和验证 xpath:

    • F12 键打开 Chrome 控制台
    • Ctrl+F,它将打开显示文本框以查找数据,确保在控制台中选择Element选项卡
    • 现在开始在此处编写自定义的 xpath,或者您可以将复制的 xpath 粘贴到此处以检查元素是否存在,它会突出显示找到的元素并显示计数。参考下图:

在此处输入图像描述

  • 查找自动生成的 xpath

    1. 右键单击您想要的元素>检查

      “在此处输入图像描述"

    2. 它将突出显示相应的元素,选择它并右键单击。它显示以下选项:

      “在此处输入图像描述"

    3. 选择复制的xpath,您将得到自动生成的xpath,您可以按照上面提到的相同方式进行评估以确保其正确

  1. 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

    //div[@id='some_id']/label/text()
    

    But selenium doesn't support to locate an element using textnode so above xpath won't work.

  2. 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:

    • Open chrome console by press F12 key
    • press Ctrl+F , it will open show textbox to find data, make sure Element tab selected in console
    • Now start writing you customized xpath here or you can paste the copied xpath here to check whether element is there for, it highlight the found element and also shows the count. Refer the below image:

enter image description here

  • To find auto generated xpath

    1. Right click on your intended element > inspect

      enter image description here

    2. It will highlight the corresponding element, select it and do right click. It show below option :

      enter image description here

    3. select the copy xpath, you will get the auto-generated xpath, you can evaluate the same as mention above to make sure its correct

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文