我想做的事情:使用Selenium 3.0.1我导航到www.spiegel.de并尝试阅读JavaScript变量inlineCampaigns
。它已经与Selenium 2.53.1一起使用。
代码:
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();
}
}
预期产量:
[ "kundenvideo", "sponsor_logo" ]
实际输出:
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)
有没有人知道这里可能出现什么问题?谢谢!任何帮助表示赞赏。
Selenium 3.0.1 Firefox 47.0.1
答案 0 :(得分:4)
对象inlineCampaigns
可能包含在window.wrappedJSObject
中:
Object result = ((JavascriptExecutor)driver).executeScript(
"return window.inlineCampaigns || (window.wrappedJSObject && window.wrappedJSObject.inlineCampaigns);");