InternetExplorerDriver fails to evaluate XPath predicate on attribute
InternetExplorerDriver seems to ignore XPath predicates on attributes, instead matching every element with that attribute and returning the first result.
Tested using Selenium 2.47.0.0.
Steps to reproduce: Using this example HTML:
<html>
<body>
<span class="foo">foo</span>
<span class="bar">bar</span>
</body>
</html>
Play back the following JUnit:
import static org.junit.Assert.*;
import java.io.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.*;
public class Example {
@Test
public void testPredicateOnAttribute() throws Throwable {
File file = new File("<path_to>\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
InternetExplorerDriver driver = new InternetExplorerDriver();
try {
driver.get("<url_to_example_page>");
WebElement bar = driver.findElement(By.xpath("//span[contains(@class,'bar')]"));
assertEquals("bar", bar.getText());
WebElement bar2 = driver.findElement(By.xpath("//span[@class[contains(.,'bar')]]"));
assertEquals("bar", bar2.getText()); // fails because 'foo' element is returned
try {
driver.findElement(By.xpath("//span[@class['a'='b']]"));
fail("Should have thrown exception"); // fails because 'foo' element is returned
} catch (NoSuchElementException e) { }
} finally {
driver.quit();
}
}
}
An equivalent test in Firefox passes as expected:
import static org.junit.Assert.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class Example {
@Test
public void testPredicateOnAttribute() throws Throwable {
FirefoxDriver driver = new FirefoxDriver();
try {
driver.get("<url_to_example_page>");
WebElement bar = driver.findElement(By.xpath("//span[contains(@class,'bar')]"));
assertEquals("bar", bar.getText());
WebElement bar2 = driver.findElement(By.xpath("//span[@class[contains(.,'bar')]]"));
assertEquals("bar", bar2.getText());
try {
driver.findElement(By.xpath("//span[@class['a'='b']]"));
fail("Should have thrown exception");
} catch (NoSuchElementException e) { }
} finally {
driver.quit();
}
}
}
While the IE driver exposes this issue, yes, it's not an issue with the IE driver per se. Rather, issue is actually in the XPath engine used by the driver, which is provided by the Wicked Good XPath project. This is easily validated by downloading that project's latest release, and using the following HTML file.
<!DOCTYPE html>
<html>
<head>
<script src="wgxpath.install.js"></script>
</head>
<body onload="wgxpath.install();">
<span class="foo">foo</span>
<span class="bar">bar</span>
</body>
</html>
Host this file on a web server, and browse to it using Internet Explorer. In the Console tab of the F12 Developer Tools, enter the following two commands, and examine the result of each one:
document.evaluate("//span[contains(@class,'bar')]", document, null, 9, null);
and
document.evaluate("//span[@class[contains(.,'bar')]]", document, null, 9, null);
The former will return the bar element; the latter will return the foo element. Furthermore, by changing the result type argument from 9 to 7 (from first node to ordered node snapshot), you can see that the first statement returns the single node, while the second returns a snapshot of two nodes, with the driver's findElement correctly returning the first one.
Evaluating the legitimacy of the XPath against the DOM Level 3 XPath spec is not something I'm prepared to do. Assuming that your XPath example, when evaluated via the rules laid out by the spec, should return the node you expect, your best bet is to raise an issue directly in the Wicked Good XPath project's issue tracker.