gpt4 book ai didi

javascript - 无法通过 FirefoxDriver 访问全局变量

转载 作者:行者123 更新时间:2023-11-29 21:15:55 24 4
gpt4 key购买 nike

我正在尝试检索 JavaScript 全局变量的值,但在 FireFox 中运行测试时总是得到 undefined

此测试在 Chrome 中成功。

index.html

<html>
<head>
<script type="text/javascript">
window.seleniumTesting = "Just A Test";
</script>
</head>
<body>
....
</body>
</html>

测试.java

WebDriver drive = new FirefoxDriver();
driver.get("http://localhost");
// Wait for the page to load. I know there are better ways of doing this.
synchronized (Thread.currentThread()) {
try {
Thread.currentThread().wait(55000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String str = (String)((JavascriptExecutor) driver).executeScript("console.info(window.seleniumTesting); return window.seleniumTesting;");
System.out.println("str: |"+str+"|");

当我运行它时,strnull 并且在浏览器的 JavaScript 控制台中 window.seleniumTesting 被记录为 undefined.

如果我使用 ChromeDriver 而不是 FirefoxDriver。一切都符合预期(strJust A Test 并且控制台记录:Just A Test)。

这似乎是一个 JavaScript 上下文问题。似乎在 FirefoxDrive 中,JavaScript 上下文不是网页的上下文。

这是一个已知问题吗?我可以将驱动程序的 JavaScript 上下文更改为网页的上下文吗?

[编辑]我正在为 OSX 使用 geckodriver

[编辑] 和 FireFox 版本 47。

最佳答案

使用 geckodriver 当前 v0.10.0,您必须使用 window.wrappedJSObject 来访问窗口对象的非标准属性:

String str = (String)((JavascriptExecutor)driver).executeScript("return window.wrappedJSObject.seleniumTesting;");

关于javascript - 无法通过 FirefoxDriver 访问全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39408015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com