2

What I am trying to do: Using Selenium 3.0.1 I navigate to www.spiegel.de and try to read the JavaScript variable inlineCampaigns. It already worked with Selenium 2.53.1.

Code:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestApp {

    private static WebDriver driver;

    private final static String geckoDriverPath = "C:\\Program Files\\geckodriver.exe";

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver", geckoDriverPath);
        driver = new FirefoxDriver();
        driver.get("http://www.spiegel.de/");
        String javaScriptCode = "return inlineCampaigns;";

        JavascriptExecutor js = null;
        if (driver instanceof JavascriptExecutor) {
            js = (JavascriptExecutor) driver;
        }
        Object result = null;
        try {
            result = js.executeScript(javaScriptCode);
        } catch (WebDriverException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            driver.quit();
            return;
        }
        if (result instanceof WebElement) {
            WebElement resultWebElement = (WebElement) result;
            System.out.println(resultWebElement.getText());
            System.out.println(String.valueOf((WebElement) result));
        } else {
            System.out.println(result);         
        }

        driver.quit();
    }

}

Expected output: [ "kundenvideo", "sponsor_logo" ]

Actual output:

ReferenceError: inlineCampaigns is not defined
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'DESKTOP-G3JOA4U', ip: '192.168.212.50', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20160623154057, version=, platform=XP, proxy={
}, command_id=1, specificationLevel=0, acceptSslCerts=false, browserVersion=47.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, tak
esScreenshot=true, takesElementScreenshot=true, platformName=Windows_NT, device=desktop}]
Session ID: a5cc6052-dcee-43ea-9311-92000bc58ef6
org.openqa.selenium.JavascriptException: ReferenceError: inlineCampaigns is not defined
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'DESKTOP-G3JOA4U', ip: '192.168.212.50', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20160623154057, version=, platform=XP, proxy={
}, command_id=1, specificationLevel=0, acceptSslCerts=false, browserVersion=47.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, tak
esScreenshot=true, takesElementScreenshot=true, platformName=Windows_NT, device=desktop}]
Session ID: a5cc6052-dcee-43ea-9311-92000bc58ef6
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
    at de.it2media.testprojekt.TestApp.main(TestApp.java:29)

Does anybody have any clue what might be the problem here? Thanks! Any help is appreciated.

Selenium 3.0.1 Firefox 47.0.1

1 Answer 1

4

The object inlineCampaigns is probably wrapped in window.wrappedJSObject:

Object result = ((JavascriptExecutor)driver).executeScript(
  "return window.inlineCampaigns || (window.wrappedJSObject && window.wrappedJSObject.inlineCampaigns);");
Sign up to request clarification or add additional context in comments.

3 Comments

That's it, thank you! Although I have no clue how I am supposed to know that. Thanks anyway though.
Well, you are not supposed to know that. You switched to the gecko driver which rely on the new marionette driver present in Firefox. This new driver doesn't fully support the Selenium specs and has many issues like this one (assuming that executeScript should behave like the console).
Helpful explanation (+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.