XPath/Selenium 无法使用包含/start-with 的部分 id 来定位元素 [英] XPath / Selenium can't locate an element using a partial id with contains / start-with

查看:30
本文介绍了XPath/Selenium 无法使用包含/start-with 的部分 id 来定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AjaxFormLoop 生成了以下 HTML.

<div class="t-forminjectortapestry-forminjector";id=rowInjector_13b87fdd8b6"><input id="number_13b87fdd8b6";名称=number_13b87fdd8b7";类型=文本"/><a id="removerowlink_13b87fdd8b6";href="#";name=removerowlink_13b87fdd8b6">remove</a>

<div class="t-forminjectortapestry-forminjector";id=rowInjector_13b87fdda70"风格=背景图像:无;背景颜色:rgb(255, 255, 251);"><input id="number_13b87fdda70";名称=number_13b87fdda70";类型=文本"/><a id="removerowlink_13b87fdda70";href="#";name=removerowlink_13b87fdda70">remove</a>

我正在尝试使用部分 ID 访问 child 2 中的第二个输入字段,但是我没有成功使其工作.

到目前为止我所尝试的.

String path = "//input[contains(@id,'number_')][2]";String path = "(//input[contains(@id,'number_')])[2]";

我什至无法使用 1 而不是 2 访问输入 1,但是如果我删除 [2] 并且只使用

<块引用>

String path = "//input[contains(@id,'number_')]";

我可以毫无问题地访问第一个字段.

如果我使用确切的 ID,我可以毫无问题地访问任一字段.

如果可能,我确实需要使用 id,因为在每个 t-forminjector 行中还有更多字段在此示例中不存在.

使用 Selenium 实现.

 final String path = "(//input[starts-with(@id,'quantity_')])[2]";新等待(){@覆盖公共布尔直到(){返回 isElementPresent(path);}}.wait(元素应该存在", TIMEOUT);


已解决

我注意到我似乎无法使用以下开头的/contains 来定位 dom 中的任何元素,但是如果我使用完整的 id,它就可以工作.

//部分ID - 失败//*[starts-with(@id,"quantity_")]//确切的ID - 有效//*[starts-with(@id,"quantity_-112409575185705")]

解决方案

除了 selenium 语法问题之外,还有一个与标记结构相关的 xpath 问题.
xpath 1: //input[starts-with(@id,'number_')][1]
xpath 2: (//input[starts-with(@id,'number_')])[1]

在下面的示例中,xpath 1 将返回 2 个节点(不正确),而 xpath 2 将是正确的,因为 input 节点不是兄弟节点,因此需要用括号来引用结果节点集

<div><input id="number_1" name="number_1" type="text"/>

<div><input id="number_2" name="number_2" type="text"/>

不带括号的结果

/>xpath//输入[starts-with(@id,'number_')][1]对象是一个节点集:集合包含 2 个节点:1 个元素输入属性 ID文本内容=number_1属性名称文本内容=number_1属性类型文本内容=文字2 元素输入属性 ID文本内容=number_2属性名称文本内容=number_2属性类型文本内容=文字

在下一个示例中,括号不会产生影响,因为节点是兄弟节点

<input id="pre_1" type="text"/><input id="pre_2" type="text"/><div>a</div>

带括号

/>xpath (//input[starts-with(@id,'pre_')])[1]对象是一个节点集:集合包含 1 个节点:1 个元素输入属性 ID文本内容=pre_1属性类型文本内容=文字

无括号

/>xpath//输入[starts-with(@id,'pre_')][1]对象是一个节点集:集合包含 1 个节点:1 个元素输入属性 ID文本内容=pre_1属性类型文本内容=文字

测试是用 xmllint shell 完成的xmllint --html --shell test.html

I have the following HTML generated with an AjaxFormLoop.

<div id="phones">
    <div class="t-forminjector tapestry-forminjector" id="rowInjector_13b87fdd8b6">
        <input id="number_13b87fdd8b6" name="number_13b87fdd8b7" type="text"/>
        <a id="removerowlink_13b87fdd8b6" href="#" name="removerowlink_13b87fdd8b6">remove</a>
    </div>
    
    <div class="t-forminjector tapestry-forminjector" id="rowInjector_13b87fdda70" style="background-image: none; background-color: rgb(255, 255, 251);">
        <input id="number_13b87fdda70" name="number_13b87fdda70" type="text" />
        <a id="removerowlink_13b87fdda70" href="#" name="removerowlink_13b87fdda70">remove</a>
    </div>
</div>

I'm trying to access the second input field in child 2 using a partial ID, however I have not been successful in getting this to work.

What I've tried thus far.

String path = "//input[contains(@id,'number_')][2]";
String path = "(//input[contains(@id,'number_')])[2]";

I can't even access input 1 using 1 instead of 2, however if I remove [2] and only use

String path = "//input[contains(@id,'number_')]";

I'm able to access the first field without issue.

If I use the exact id, I'm able to access either field without issue.

I do need to use the id if possible as there is many more fields in each t-forminjector row that are not present in this example.

Implementation with Selenium.

    final String path = "(//input[starts-with(@id,'quantity_')])[2]";
    
    new Wait() {
        @Override
        public boolean until() {
            return isElementPresent(path);
        }
    }.wait("Element should be present", TIMEOUT);


Resolved

I'm noticing I can't seem to use the following starts-with / contains to locate any element within to dom, however if I use a complete id, it works.

//Partial ID - fails
//*[starts-with(@id,"quantity_")]

//Exact ID - works
//*[starts-with(@id,"quantity_-112409575185705")]

解决方案

Besides the selenium syntax problem there's an xpath issue related to markup structure.
xpath 1: //input[starts-with(@id,'number_')][1]
xpath 2: (//input[starts-with(@id,'number_')])[1]

In the sample below xpath 1 will return 2 nodes (incorrect) and xpath 2 will be correct because input nodes are not siblings so surrounding parenthesis are needed to refer to the resulting nodeset

<div id="phones">
    <div>
        <input id="number_1" name="number_1" type="text"/>
    </div>
    <div>
        <input id="number_2" name="number_2" type="text" />
    </div>
</div>

Result without parenthesis

/ > xpath //input[starts-with(@id,'number_')][1]
Object is a Node Set :
Set contains 2 nodes:
1  ELEMENT input
    ATTRIBUTE id
    TEXT
        content=number_1
    ATTRIBUTE name
    TEXT
        content=number_1
    ATTRIBUTE type
    TEXT
        content=text
2  ELEMENT input
    ATTRIBUTE id
    TEXT
        content=number_2
    ATTRIBUTE name
    TEXT
        content=number_2
    ATTRIBUTE type
    TEXT
        content=text

In this next sample, parenthesis will not make a difference because nodes are siblings

<div id="other">
<input id="pre_1" type="text"/>
<input id="pre_2" type="text" />
<div>a</div>
</div>

With parenthesis

/ > xpath (//input[starts-with(@id,'pre_')])[1]
Object is a Node Set :
Set contains 1 nodes:
1  ELEMENT input
    ATTRIBUTE id
    TEXT
        content=pre_1
    ATTRIBUTE type
    TEXT
        content=text

Without parenthesis

/ > xpath //input[starts-with(@id,'pre_')][1]
Object is a Node Set :
Set contains 1 nodes:
1  ELEMENT input
    ATTRIBUTE id
    TEXT
        content=pre_1
    ATTRIBUTE type
    TEXT
        content=text

Testing was done with xmllint shell xmllint --html --shell test.html

这篇关于XPath/Selenium 无法使用包含/start-with 的部分 id 来定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆