简体   繁体   English

如何在网页上查找文本并使用Selenium和Java获取其XPath或CSS

[英]How to find text on a webpage and get its XPath or CSS with Selenium and Java

There is a page that lists items on a dynamic table. 有一个页面列出了动态表中的项目。 The order and the number of items on the table changes randomly. 桌子上的物品顺序和数量随机变化。

I would like to find one of the items "Itemx" based on the text "Itemx" and then get and use its XPath or CSS in another part of the code. 我想基于文本“ Itemx”找到项目“ Itemx”之一,然后在代码的另一部分中获取并使用其XPath或CSS。

I can find the element based on the text "Itemx" 我可以基于文本“ Itemx”找到该元素

driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Itemx[\\s\\S]*$");

But how can I get its XPath or CSS? 但是,如何获得其XPath或CSS?

I can't seem to find anything on the web. 我似乎在网上找不到任何东西。 Everything out there shows you how to verify or find the text using XPath. 那里的所有内容都向您展示了如何使用XPath验证或查找文本。 For me it's the opposite. 对我来说恰恰相反。 I have the text and I want to find its XPath. 我有文本,我想找到它的XPath。

Both XPath and CSS selectors are arbitrary. XPath和CSS选择器都是任意的。 There is no good answer for this in the general case. 在一般情况下,没有好的答案。

Selecting the first c in <a><b></b><c></c><c></c></a> with a CSS selector might be c:first-of-type , or a > c:nth-child(2) , or ac:first-of-type , or a > b + c , or a number of other things. 选择第一c<a><b></b><c></c><c></c></a>与CSS选择可能是c:first-of-type ,或a > c:nth-child(2)ac:first-of-type ,或a > b + c ,或其他一些东西。 Similarly as an XPath there are many ways of representing it. 与XPath类似,有很多表示它的方法。 If you change the markup, what should happen? 如果您更改标记,应该怎么办?

It all depends upon the context, the stability required, whether false positives or false negatives are worse (you can make it very strict so that if the markup changes it will probably break, or you can make it very loose so that if the markup changes you may well select the wrong thing) and the whim of the person doing it. 这完全取决于上下文,所需的稳定性,错误肯定或错误否定是否更糟(您可以将其设置得非常严格,以便如果标记更改,它可能会破坏,或者可以使其变得非常宽松,以便如果标记更改,您很可能会选择错误的内容)以及执行此操作的人的想法。 (Which of the ways above will I select? Eeny, meeny, miny, mo...) (我会选择上述哪种方式?Eeny,meeny,miny,mo ...)

If, however, you only care about the current state of a document and have no need for resilience, first of all consider if you actually need to have such a selector. 但是,如果您只关心文档的当前状态并且不需要弹性,则首先考虑您是否确实需要这样的选择器。 You can probably pass the DOM element around instead. 您可能可以改为传递DOM元素。 If you want to uniquely identify an element, though, following through the DOM tree is going to be a fairly straightforward way, travelling down level by level, observing which child the node is. 但是,如果要唯一地标识元素,则遵循DOM树将是一种非常简单的方法,逐级向下移动,观察节点是哪个子节点。 You'll end up with something like this: 您将得到如下所示的结果:

:root > :nth-child(3) > :nth-child(1) > :nth-child(4)

Ugly, but it may be what you want. 丑陋,但这可能就是您想要的。

如果可以通过文本识别项目,则已经有一个XPath: //*[contains(., 'Itemx')

There's an add-on for Firefox's firebug called Firepath. Firefox的Firebug有一个名为Firepath的加载项。 Basically with it installed, you right click on any element on a page and select: inspect in FirePath. 基本上安装了它,您可以右键单击页面上的任何元素,然后选择:在FirePath中检查。 You can also run your tests in here too, if you're trying to modify the XPATH or CSS SELECTOR path shorter, etc. 如果您尝试将XPATH或CSS SELECTOR路径修改得更短,也可以在此处运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM