Selenium RC 表的 XPath 存在问题

发布于 2024-08-05 02:08:01 字数 925 浏览 2 评论 0原文

我正在尝试选择由以下给出的元素:

/html/body[@id='someid']/form[@id='formid']/div[@id='someid2']/div[@id='']/div[@id='']/div[@id='']/table/tbody[@id='tableid']/tr[7]/td[2]

现在,我尝试选择的该行的 html 看起来像这样:

<tr>
<td class="someClass">some text</td>
<td class="someClass2">my required text for verifying</td>
</tr>

我需要检查页面中是否存在用于验证的所需文本

  1. 我使用了 selenium.isTextPresent("我需要验证的文本"); 但它不起作用

  2. 所以现在我尝试使用 selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")
    这有时有效,但偶尔会出现随机失败。

  3. 也尝试过selenium.isElementPresent(//*[contains(text(),'my required text for verifying')])..

如何使用 selenium 验证页面上的此文本?

问题不在于页面加载需要时间。我在故障发生之前截取了屏幕截图,发现页面已完全加载,因此应该不是问题。

有人可以建议任何方式来选择此元素或任何方式来验证屏幕上的此文本吗?

I'm trying to select an element given by:

/html/body[@id='someid']/form[@id='formid']/div[@id='someid2']/div[@id='']/div[@id='']/div[@id='']/table/tbody[@id='tableid']/tr[7]/td[2]

Now the html of that row I'm trying to select looks like this:

<tr>
<td class="someClass">some text</td>
<td class="someClass2">my required text for verifying</td>
</tr>

I need to check whether my required text for verifying exists in the page.

  1. I used selenium.isTextPresent("my required text for verifying"); and it doesnt work

  2. So now I tried with selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")

    This works sometimes but occassionally gives random failures.

  3. Tried with selenium.isElementPresent(//*[contains(text(),'my required text for verifying')]) too..

How do I verify this text on the page using selenium?

The problem is not with the page taking time to load. I took screenshots before the failure occurs and found that the page was fully loaded so that shouldnt be the problem.

Could someone please suggest any way to select this element or any way to validate this text on the screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

好多鱼好多余 2024-08-12 02:08:01

尝试通过 CSS 定位它:

assertText(selenium.getText("css=.someClass2"), "my required text for verifying");

上面应该给出比 isElementPresent 更好的失败消息,但您仍然可以将其与 CSS 定位器一起使用:

assertTrue(selenium.isElementPresent("css=.someClass2"));

如果加载时间有问题,您可以尝试等待元素出现:

selenium.waitForCondition("var value = selenium.isElementPresent('css=.someClass2'); value == true", "60000");

其他一些 XPath如果您不想使用 CSS 定位器,可能适合您的定位器:

  • //td[contains(@class, 'someClass2')
  • xpath=id('tableid')/tr[7]/td[2]
  • xpath=id ('tableid')/后代::td[包含(@class, 'someClass2')][7]

Try locating it by CSS:

assertText(selenium.getText("css=.someClass2"), "my required text for verifying");

The above should give a better failure message than isElementPresent, but you can still use that with CSS locators:

assertTrue(selenium.isElementPresent("css=.someClass2"));

If there is an issue with the load times you could try waiting for the element to be present:

selenium.waitForCondition("var value = selenium.isElementPresent('css=.someClass2'); value == true", "60000");

Some other XPath locators that might work for you, if you prefer not to use CSS locators:

  • //td[contains(@class, 'someClass2')
  • xpath=id('tableid')/tr[7]/td[2]
  • xpath=id('tableid')/descendant::td[contains(@class, 'someClass2')][7]
旧梦荧光笔 2024-08-12 02:08:01

我从未听说过硒;但您最初的 XPath 过于脆弱和冗长。

如果一个元素有一个 id,那么它是唯一的;使用这么长的 XPath 只是为了选择特定元素是没有必要的;只需选择带有 id 的最后元素即可。此外,我发现您偶尔会选择 xyz[@id=''] - 如果您尝试选择没有 id 属性的元素,您可以执行“xyz” [不是(@id)] 相反。

假设您的初始 XPath 基本上是正确的,那么执行以下操作就足够了:

//tbody[@id='tableid']/tr[7]/td[2]

但是,如果无论如何更改 html 的详细信息,使用这样的特定行号和列号就会带来麻烦。另外,tbody 元素上有 id 是不典型的,也许 table 元素有 id?

最后,您可能会遇到空间标准化问题。在 xml 中,多个连续空格通常被认为等同于单个空格,而您没有考虑到这一点。特别是,如果 xhtml 打印得很漂亮并且在您想要的文本中间包含换行符,那么它就不起作用。

//td[contains(normalize-space(text()),'my required text for verifying')]

最后,text()显式选择
文本节点 - 因此上面的 xpath 不会选择文本不是 td 直接子节点的元素(例如 我用于验证的所需文本< /b>) 不匹配。也许您的意思是查找所有后代的串联文本值:

//td[contains(normalize-space(string(.)),'my required text for verifying')]

最后,类型转换可以在 XPath 中隐式转换,因此 string(.) 可以替换为 .上面,导致版本:

//td[contains(normalize-space(.),'my required text for verifying')]

这在大型文档上可能会很慢,因为它需要标准化空格并对每个 td 元素执行字符串搜索。如果遇到性能问题,请尝试更具体地说明需要检查哪些 td 元素,或者,如果您不关心文本出现的位置,请尝试减少“调用”的次数通过一次性规范化整个文档来规范化空间(例如通过 /*[contains(normalize-space(.),'my required text for verifying')])。

I've never heard of selenium; but your initial XPath is unnecessarily fragile and verbose.

If an element has an id, it's unique; using such a long XPath just to select a particular element is unnecessary; just select the last element with the id. Further, I see that you're occasionally selecting xyz[@id=''] - if you're trying to select elements without id attributes, you can do `xyz[not(@id)] instead.

Assuming your initial XPath is basically correct, it would suffice to do something like this:

//tbody[@id='tableid']/tr[7]/td[2]

However, using a specific row and column number like that is asking for trouble if ever anyhow changes details of the html. Also, it's atypical to have id's on tbody elements, perhaps the table element has the id?

Finally, you may be running into space-normalization issues. In xml, multiple consecutive spaces are often considered equivalent to a single space, and you're not accounting for that. In particular, if the xhtml is pretty-printed and contains a line-break in the middle of your sought-after text, it won't work.

//td[contains(normalize-space(text()),'my required text for verifying')]

Finally, text() explicitly selects
child text nodes - so the above xpath won't select elements where the text isn't the immediate child of td (e.g. <td><b>my required text for verifying</b></td>) won't match. Perhaps you mean to look up the concatenated text vale of all descendents:

//td[contains(normalize-space(string(.)),'my required text for verifying')]

Finally, type conversion can be implicit in XPath, so string(.) can be replaced by . in the above, leading to the version:

//td[contains(normalize-space(.),'my required text for verifying')]

This may be slow on large documents since it needs to normalize the spaces and perform a string search for each td element. If you run into perf problems, try to be more specific about which td elements need to be inspected, or, if you don't care where the text occurs, try to reduce the number of "calls" to normalize-space by normalizing the entire doc in one go (e.g. via /*[contains(normalize-space(.),'my required text for verifying')]).

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