SeleniumHQ / selenium-google-code-issue-archive

Archive, please see main selenium repo
https://github.com/seleniumhq/selenium
345 stars 195 forks source link

XPath expressions stopped working in the latest selenium release #2014

Closed lukeis closed 8 years ago

lukeis commented 8 years ago

Originally reported on Google Code with ID 2014

What steps will reproduce the problem?
Please run following using c# bindings

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;

using OpenQA.Selenium;

namespace NewSeleniumTest
{
    class Program
    {
        private static IWebDriver driver;

        static void Main(string[] args)
        {
            driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.easyjet.co.uk");

            S("orig", "Lon");
            S("dest", "Par");

            S("oMonYear", Keys.Down);
            S("oMonYear", Keys.Down);

            C("btn_submitForm");

            C("priceSmaller");

            Thread.Sleep(2000);

            var padlockImage = GetEl("PadlockImage");

            // To avoid "chasing" shopping basket all over the page
            var jsExec = driver as IJavaScriptExecutor;
            jsExec.ExecuteScript( @"var fireOnThis = arguments[0];
                  var evt = 'click';
                  if (document.createEvent) {
                    var evObj = document.createEvent('MouseEvents');
                    evObj.initEvent(evt, true, false);
                    fireOnThis.dispatchEvent(evObj);
                  } else if (document.createEventObject) {
                    fireOnThis.fireEvent('on'+evt);
                  }",
                  padlockImage);

            C("btnContinue");

            C("btnAddTravelInsurance0");
            Thread.Sleep(2000);
            C("addDefaultHoldBaggage");
            Thread.Sleep(2000);
            C("btnContinue");

            var hotelItem = GetEl("HotelListItem");

            var link = hotelItem.FindElement(By.XPath(".//a"));
            Console.WriteLine(link.Text);
        }

        private static void C(string identify)
        {
            var el = GetEl(identify);
            el.Click();
        }

        private static IWebElement GetEl(string identify)
        {
            ReadOnlyCollection<IWebElement> els;

            els = driver.FindElements(By.Id(identify));
            if (els.Count > 0) return els.First();

            els = driver.FindElements(By.Name(identify));
            if (els.Count > 0) return els.First();

            els = driver.FindElements(By.ClassName(identify));
            if (els.Count > 0) return els.First();

            els = driver.FindElements(By.XPath(identify));
            if (els.Count > 0) return els.First();

            return null;
        }

        private static void S(string id, string value)
        {
            GetEl(id).SendKeys(value);
        }
    }
}

What is the expected output? What do you see instead?
link object found, instead, we get following exception:

  ----> OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"xpath","selector":".//a"}

Selenium version: 2.0.0
OS: Windows 7 x86
Browser: Firefox
Brower version: 4 and 5

Reported by stan.wozniak on 2011-07-08 14:42:33

lukeis commented 8 years ago
This is still an issue in Selenium 2.1 CSharp bindings using local browser or remote
web driver.

Reported by stan.wozniak on 2011-07-19 07:57:39

lukeis commented 8 years ago
I have managed to replicate the same issue in the latest version of selenium this time
in Java. Here is the script and the output:

Script:

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: stanw
 * Date: 12/08/11
 * Time: 16:55
 * To change this template use File | Settings | File Templates.
 */
public class Tests {

    private static WebDriver driver;

    @Test
    public void WebDriverTest() throws InterruptedException {
        driver = new FirefoxDriver();
        driver.navigate().to("http://www.easyejt.com/EN");

        S("orig", "*LO");
        S("dest", "CDG");

        S("oMonYear", "122011");

        C("btn_submitForm");

        C("priceSmaller");

        WebElement padlockImage = GetEl("PadlockImage");

        // To avoid "chasing" shopping basket all over the page
        JavascriptExecutor jsExec = ((JavascriptExecutor)driver);

        jsExec.executeScript("var fireOnThis = arguments[0];\n" +
                "var evt = 'click';\n" +
                "if (document.createEvent) {\n" +
                "  var evObj = document.createEvent('MouseEvents');\n" +
                "  evObj.initEvent(evt, true, false);\n" +
                "  fireOnThis.dispatchEvent(evObj);\n" +
                "} else if (document.createEventObject) {\n" +
                "  fireOnThis.fireEvent('on'+evt);\n" +
                "}",
                padlockImage);

        C("btnContinue");

        C("btnAddTravelInsurance0");

        Thread.sleep(2000);
        C("addDefaultHoldBaggage");
        Thread.sleep(2000);
        C("btnContinue");

        WebElement hotelItem = GetEl("HotelListItem");

        WebElement link = hotelItem.findElement(By.xpath(".//a"));

        System.out.println(link.getText());
    }

    private static void C(String identify)
    {
        WebElement el = GetEl(identify);
        el.click();
    }

    private static WebElement GetEl(String identify)
    {
        List<WebElement> els;

        els = driver.findElements(By.id(identify));
        if (els.size() > 0) return els.get(0);

        els = driver.findElements(By.name(identify));
        if (els.size() > 0) return els.get(0);

        els = driver.findElements(By.className(identify));
        if (els.size() > 0) return els.get(0);

        els = driver.findElements(By.xpath(identify));
        if (els.size() > 0) return els.get(0);

        return null;
    }

    private static void S(String id, String value)
    {
        WebElement parent = GetEl(id);

        List<WebElement> options = parent.findElements(By.tagName("option"));

        WebElement matchingOption = null;

        for(WebElement option : options) {
            if(option.getAttribute("value").equalsIgnoreCase(value)) {
                matchingOption = option;
                break;
            }
        }

        if(matchingOption != null)
            matchingOption.click();
    }
}

Output:

"C:\Program Files (x86)\Java\jdk1.6.0_21\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\Program
Files (x86)\JetBrains\IntelliJ IDEA Community Edition 10.5.1\bin" -Dfile.encoding=windows-1252
-classpath "D:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 10.5.1\lib\idea_rt.jar;D:\Program
Files (x86)\JetBrains\IntelliJ IDEA Community Edition 10.5.1\plugins\junit\lib\junit-rt.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\alt-rt.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\javaws.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\jsse.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\plugin.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\ext\localedata.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\ext\sunmscapi.jar;C:\Program
Files (x86)\Java\jdk1.6.0_21\jre\lib\ext\sunpkcs11.jar;D:\JavaProjects\SeleniumFirefoxXPathTest\out\production\com.sponte.selenium;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\apache-mime4j-0.6.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\bsh-1.3.0.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\cglib-nodep-2.1_3.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-codec-1.4.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-collections-3.2.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-io-2.0.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-jxpath-1.3.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-lang-2.4.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\commons-logging-1.1.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\cssparser-0.9.5.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\guava-r09.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\hamcrest-all-1.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\htmlunit-2.8.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\htmlunit-core-js-2.8.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\httpclient-4.0.2.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\httpcore-4.0.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\httpmime-4.0.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\jcommander-1.13.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\jna-3.3.0.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\json-20080701.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\junit-dep-4.8.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\nekohtml-1.9.14.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\operadriver-v0.6.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\protobuf-java-2.3.0.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\sac-1.3.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\serializer-2.7.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\testng-6.0.1-nobsh-noguice.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\xalan-2.7.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\xercesImpl-2.9.1.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\libs\xml-apis-1.3.04.jar;C:\Users\stanw\Downloads\selenium-java-2.4.0\selenium-2.4.0\selenium-java-2.4.0.jar"
com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter
-ideVersion5 -junit4 Tests,WebDriverTest

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//a"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.4.0', revision: '13337', time: '2011-08-12 09:58:05'
System info: os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1',
java.version: '1.6.0_21'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:407)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:213)
    at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:136)
    at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:189)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:323)
    at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:132)
    at Tests.WebDriverTest(Tests.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to
locate element: {"method":"xpath","selector":".//a"}
Build info: version: '2.4.0', revision: '13337', time: '2011-08-12 09:58:05'
System info: os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1',
java.version: '1.6.0_21'
Driver info: driver.version: unknown
    at <anonymous class>.<anonymous method>(file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js
-> file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/firefoxDriver.js:387)
    at <anonymous class>.<anonymous method>(file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js
-> file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/firefoxDriver.js:427)
    at <anonymous class>.<anonymous method>(file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/nsCommandProcessor.js:306)
    at <anonymous class>.<anonymous method>(file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/nsCommandProcessor.js:320)
    at <anonymous class>.<anonymous method>(file:///C:/Users/stanw/AppData/Local/Temp/2/anonymous2508448810931764435webdriver-profile/extensions/fxdriver@googlecode.com/components/nsCommandProcessor.js:197)

Process finished with exit code -1

Reported by stan.wozniak on 2011-08-12 16:26:03

lukeis commented 8 years ago
Hi,

This is still not fixed in 2.6.0. We are forced to use FF 3.6 to run our tests. Soon
this browser version is not going to be supported.

This only affects calling FindElement By.XPath on IWebElement. It works OK on the driver
instance.

Please let me know if you need more information to debug/fix this bug.

Kind regards,
Stan

Reported by stan.wozniak on 2011-09-14 13:33:21

lukeis commented 8 years ago

Reported by barancev on 2011-10-13 06:25:30

lukeis commented 8 years ago
We have a lot of tests that show this works in all browsers - please submit a new issue
with a failing test case attached if you're still having problems

Reported by dawagner on 2012-04-11 22:52:30

lukeis commented 8 years ago

Reported by luke.semerau on 2015-09-17 18:13:10