简体   繁体   English

Xpath不适用于Selenium

[英]Xpath does not work with Selenium

I'm trying to build tests for an old system. 我正在尝试为旧系统构建测试。 The HTML is not well formed. HTML格式不正确。 I need to identify and click a radio button. 我需要识别并单击一个单选按钮。

The html looks like this: html看起来像这样:

...
<td class="tablerow" colspan="3">
   <INPUT type=radio name="ticket" value="22" >ramdom1
   <INPUT type=radio name="ticket" value="1" >ramdom2
   <INPUT type=radio name="ticket" value="3" >ramdom3
   <INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...

I was trying to select the input using xpath as follows: 我试图使用xpath选择输入,如下所示:

String xpath = "//input[contains(@name, 'ticket') and contains(@value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));

But selenium doesn't found the element. 但是硒没有找到这种元素。

If change it to 如果改为

String xpath = "//input[contains(@name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));

or 要么

String xpath = "//input[contains(@value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));

It works, selenium returns a list of elements, including the one that I need. 它有效,selenium返回一个元素列表,包括我需要的元素。 The problem occurs only when I try to use both conditions in a same xpath. 只有当我尝试在同一个xpath中使用这两个条件时才会出现此问题。

Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. 当然,我可以迭代列表并测试每个值,但我想了解我是否做错了。 Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue. 由于IE没有本机xpath支持,我认为这可能是一个selenium实现问题。

I'm using Selenium WebDriver (2.37.1) with IE Driver. 我正在使用Selenium WebDriver(2.37.1)和IE Driver。

Not sure whether this is a Selenium implementation issue but this should work: 不确定这是否是Selenium实现问题,但这应该有效:

"//input[contains(@name, 'ticket')][contains(@value, '3')]"

The use of and is basically the same so the result should be correct here. 使用and基本相同所以结果在这里应该是正确的。

I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault. 我不确定为什么这不起作用,这在技术上不是一个答案,但你可以准确地复制Selenium所做的事情,以确保它不是Selenium或其任何工具的错误。

Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one. Selenium使用一个名为“Wicked Good XPath”的库来实现基于Javascript的XPath引擎实现,因为IE没有“本机”实现。

So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. 因此,要重现场景,请复制页面并将Wicked Good XPath添加到脚本标题中。 Documentation on the front page of that website is very simple, and very easy to follow. 该网站首页上的文档非常简单,非常容易理解。

Once loaded in IE, open the Developer Tools and go into the Console. 在IE中加载后,打开开发人员工具并进入控制台。 Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console. Wicked Good XPath需要“初始化”,因此你需要在控制台中调用wgxpath.install()

Once done, you now have access to the same library that Selenium would be using. 完成后,您现在可以访问Selenium将使用的相同库。 Now, you can call a function within IE's developer console to access the DOM using XPath: 现在,您可以在IE的开发人员控制台中调用一个函数来使用XPath访问DOM:

document.evaluate("//input[contains(@name, 'ticket') and contains(@value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue

The exact element you need will be returned, at least for me. 你需要的确切元素将被退回,至少对我而言。

Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors: 现在,诚然,你根本不需要XPath,你可以使用CSS选择器:

input[name~='ticket'][value='3']

We can use the following css 我们可以使用以下css

1.css=input[value='22'] 1.CSS =输入[值= '22' ]

2.css=input[value='1'] 2.css =输入[值= '1']

3.css=input[value='3'] 3.css =输入[值= '3']

4.css=input[value='99'] 4.css =输入[值= '99' ]

For Checking the Radio Buttons. 检查单选按钮。

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

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