Xpath不适用于Selenium [英] Xpath does not work with Selenium

查看:142
本文介绍了Xpath不适用于Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为旧系统构建测试. HTML格式不正确.我需要识别并单击一个单选按钮.

I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.

html看起来像这样:

The html looks like this:

...
<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>
...

我试图使用xpath选择输入,如下所示:

I was trying to select the input using xpath as follows:

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

但是硒找不到元素.

如果将其更改为

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

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

它的工作原理是,硒返回一系列元素,包括我需要的元素.仅当我尝试在同一个xpath中同时使用两个条件时,才会出现此问题.

It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.

我当然可以遍历列表并测试每个值,但是我想了解我是否做错了什么.由于IE没有本机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.

我正在将Selenium WebDriver(2.37.1)与IE驱动程序配合使用.

I'm using Selenium WebDriver (2.37.1) with IE Driver.

推荐答案

不确定这是否是Selenium实现问题,但这应该可以解决:

Not sure whether this is a Selenium implementation issue but this should work:

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

and的用法基本相同,因此此处的结果应该正确.

The use of and is basically the same so the result should be correct here.

这篇关于Xpath不适用于Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆