简体   繁体   English

我无法使用XPATH找到与子元素完全匹配的对象

[英]I am not able to find an exact match for child elements using XPATH

My requirement is to send a string into the webdriver and match the string with the label for the radio button. 我的要求是将一个字符串发送到webdriver中,并将该字符串与单选按钮的标签匹配。

<input type="radio" onclick="Element.show('indicator_radio_term_190');render_selected_term('190','1','275467','AQCB Number')" value="190" name="radio_190"/>
<input type="radio" onclick="Element.show('indicator_radio_term_179');render_selected_term('179','1','275467','AQCB Number (iLink)')" value="179" name="radio_179"/>

The above is the structure of two radio buttons with names " AQCB Number " and " AQCB Number (iLink) ". 上面是两个名为“ AQCB Number ”和“ AQCB Number (iLink) ”的单选按钮的结构。 So when I hand over the string "AQCB Number" I then want to find the Radio Button with that exact same text. 因此,当我将字符串“ AQCB号”移交给我时,我想找到带有完全相同的文本的单选按钮。

When i use //input[contains(@onclick,'AQCB Number')] to fetch the xpath with the label, i get both the results which is throwing an exception. 当我使用//input[contains(@onclick,'AQCB Number')]来获取带有标签的xpath时,我都得到了抛出异常的两个结果。 Is there a way where i can find an exact match and still use contains? 有没有一种方法可以找到完全匹配并仍然使用包含? " AQCB Number " and " AQCB Number (iLink) " are the labels of the radio button. AQCB Number ”和“ AQCB Number (iLink) ”是单选按钮的标签。

Include the single quote too: 也包括单引号:

driver.findElement(By.xpath("//input[contains(@onclick, \"'AQCB Number'\")]"));

or 要么

driver.findElement(By.xpath("//input[contains(@onclick, '\'AQCB Number\'')]"));

Regarding your question is not specifically about how to distinguish your AQCB-entries but how you can match the exact child you want 关于您的问题不是专门关于如何区分您的AQCB条目的问题,而是有关您如何匹配所需的确切孩子的问题

I suggest using the value or name attribute to get the match in a more readable way : 我建议使用value或name属性以更易读的方式获取匹配项:

first by name : 首先按名称

driver.findElement(By.name("radio_190"));

or by value : 按值

driver.findElement(By.xpath("//input[@value = '190']"));

UPDATE 更新

After clarification from the OP, what his actual requirement is, then the solution of @peetya is of course correct: 在OP澄清之后,他的实际要求是什么,那么@peetya的解决方案当然是正确的:

You just need to also use the quotation marks which are found in the js code of the onclick event: 您只需要使用在onclick事件的js代码中找到的引号即可:

driver.findElement(By.xpath("//input[contains(@onclick, '\'AQCB Number\'')]"));

To go further, if you are handing over a String "radioButtonName" you should do something like this then: 更进一步,如果您要传递字符串“ radioButtonName”,则应执行以下操作:

String searchString = "'" + radioButtonName + "'";

driver.findElement(By.xpath("//input[contains(@onclick, '" + searchString + "')]"));

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

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