Selenium RC 表的 XPath 存在问题
我正在尝试选择由以下给出的元素:
/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>
我需要检查页面中是否存在用于验证的所需文本。
我使用了
selenium.isTextPresent("我需要验证的文本");
但它不起作用所以现在我尝试使用
selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")
这有时有效,但偶尔会出现随机失败。也尝试过
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.
I used
selenium.isTextPresent("my required text for verifying");
and it doesnt workSo now I tried with
selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")
This works sometimes but occassionally gives random failures.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试通过 CSS 定位它:
上面应该给出比 isElementPresent 更好的失败消息,但您仍然可以将其与 CSS 定位器一起使用:
如果加载时间有问题,您可以尝试等待元素出现:
其他一些 XPath如果您不想使用 CSS 定位器,可能适合您的定位器:
Try locating it by CSS:
The above should give a better failure message than isElementPresent, but you can still use that with CSS locators:
If there is an issue with the load times you could try waiting for the element to be present:
Some other XPath locators that might work for you, if you prefer not to use CSS locators:
我从未听说过硒;但您最初的 XPath 过于脆弱和冗长。
如果一个元素有一个 id,那么它是唯一的;使用这么长的 XPath 只是为了选择特定元素是没有必要的;只需选择带有 id 的最后元素即可。此外,我发现您偶尔会选择
xyz[@id='']
- 如果您尝试选择没有 id 属性的元素,您可以执行“xyz” [不是(@id)] 相反。假设您的初始 XPath 基本上是正确的,那么执行以下操作就足够了:
但是,如果无论如何更改 html 的详细信息,使用这样的特定行号和列号就会带来麻烦。另外,
tbody
元素上有 id 是不典型的,也许table
元素有 id?最后,您可能会遇到空间标准化问题。在 xml 中,多个连续空格通常被认为等同于单个空格,而您没有考虑到这一点。特别是,如果 xhtml 打印得很漂亮并且在您想要的文本中间包含换行符,那么它就不起作用。
最后,
text()
显式选择子文本节点 - 因此上面的 xpath 不会选择文本不是 td 直接子节点的元素(例如
我用于验证的所需文本< /b>
) 不匹配。也许您的意思是查找所有后代的串联文本值:最后,类型转换可以在 XPath 中隐式转换,因此
string(.)
可以替换为.
上面,导致版本:这在大型文档上可能会很慢,因为它需要标准化空格并对每个 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:
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 thetable
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.
Finally,
text()
explicitly selectschild 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:Finally, type conversion can be implicit in XPath, so
string(.)
can be replaced by.
in the above, leading to the version: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 whichtd
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')]
).