Skip to content

Commit 4f5f96d

Browse files
committed
Taking XML namespaces into account when searching by XPath. Checked to work in Firefox. Chrome supports namespaces out of the box. Need to update IE and Safari drivers to use the updated atom and test them carefully.
1 parent d1f5eff commit 4f5f96d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

java/client/test/org/openqa/selenium/ElementFindingTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
import static org.junit.Assume.assumeFalse;
3333
import static org.openqa.selenium.testing.Ignore.Driver.ANDROID;
3434
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
35+
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
3536
import static org.openqa.selenium.testing.Ignore.Driver.IE;
3637
import static org.openqa.selenium.testing.Ignore.Driver.IPHONE;
3738
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;
3839
import static org.openqa.selenium.testing.Ignore.Driver.OPERA;
3940
import static org.openqa.selenium.testing.Ignore.Driver.OPERA_MOBILE;
4041
import static org.openqa.selenium.testing.Ignore.Driver.REMOTE;
42+
import static org.openqa.selenium.testing.Ignore.Driver.SAFARI;
4143
import static org.openqa.selenium.testing.TestUtilities.isOldIe;
4244

4345
import static org.hamcrest.Matchers.is;
@@ -399,6 +401,14 @@ public void testFindingALinkByXpathUsingContainsKeywordShouldWork() {
399401
assertThat(element.getText(), containsString("hello world"));
400402
}
401403

404+
@Ignore({ANDROID, HTMLUNIT, IE, IPHONE, OPERA, OPERA_MOBILE, MARIONETTE, SAFARI})
405+
@Test
406+
public void testShouldBeAbleToFindElementByXPathWithNamespace() {
407+
driver.get(pages.svgPage);
408+
WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));
409+
assertThat(element.getText(), is("Test Chart"));
410+
}
411+
402412
// By.xpath negative
403413

404414
@Test(expected = NoSuchElementException.class)

javascript/atoms/locators/xpath.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,31 @@ bot.locators.xpath.evaluate_ = function(node, path, resultType) {
9696
}
9797

9898
try {
99-
var resolver = doc.createNSResolver ?
100-
doc.createNSResolver(doc.documentElement) :
101-
bot.locators.xpath.DEFAULT_RESOLVER_;
99+
var reversedNamespaces = {};
100+
var allNodes = doc.getElementsByTagName("*");
101+
for (var i = 0; i < allNodes.length; ++i) {
102+
var node = allNodes[i];
103+
var ns = node.namespaceURI;
104+
if (!reversedNamespaces[ns]) {
105+
var prefix = node.lookupPrefix(ns);
106+
if (!prefix) {
107+
var m = ns.match('.*/(\\w+)/?$');
108+
if (m) {
109+
prefix = m[1];
110+
} else {
111+
prefix = 'xhtml';
112+
}
113+
}
114+
reversedNamespaces[ns] = prefix;
115+
}
116+
}
117+
var namespaces = {};
118+
for (var key in reversedNamespaces) {
119+
namespaces[reversedNamespaces[key]] = key;
120+
}
121+
var resolver = function(prefix) {
122+
return namespaces[prefix] || null;
123+
}
102124
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(7)) {
103125
// IE6, and only IE6, has an issue where calling a custom function
104126
// directly attached to the document object does not correctly propagate

0 commit comments

Comments
 (0)