简体   繁体   中英

Xpath does not work with Selenium

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.

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

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));

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. The problem occurs only when I try to use both conditions in a same 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.

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

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

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

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

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 uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.

So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. 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. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.

Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:

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:

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

We can use the following css

1.css=input[value='22']

2.css=input[value='1']

3.css=input[value='3']

4.css=input[value='99']

For Checking the Radio Buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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