带有 firefox 的 selenium 按 javascript 关闭选项卡但 SetTimeout() 不正常
selenium with firefox close tab by javascript but SetTimeout() not ok
我使用多线程方式打开选项卡,当页面完成加载时关闭选项卡
这是我的打开标签代码
((JavascriptExecutor)webDriver).executeScript("window.open('"+url+"')");
关闭选项卡时,我迭代 webDriver.getWindowHandles()
并使用函数 executeScript
,但发现一些错误,这是我的代码
此代码在 chrome 中工作正常,它关闭所有选项卡,但在 firefox 中一些 windowHandler 被关闭而一些没有
((JavascriptExecutor) webDriver).executeScript("setTimeout(function(){window.close()},10000)");
或
((JavascriptExecutor) webDriver).executeScript("setInterval(function(){window.close()},10000)");
我尝试使用这段代码,但它也像上面的代码
((JavascriptExecutor) webDriver).executeScript("var script = document.createElement('script');\n" +
"script.type = 'text/javascript';\n" +
"script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" +
"script.innerHTML = 'window.close()';\n" +
"document.body.appendChild(script);");
但是当我删除 "script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" +
时只使用 window.close()
它可以关闭所有选项卡,但不符合需要
所以我认为可能 setTimeout
和 setInterval
函数在 firefox
中导致了这个问题
然后我将代码更改为
((JavascriptExecutor) webDriver).executeScript("var start = null;\n" +
"\n" +
"function step(timestamp) {\n" +
" if (!start) start = timestamp;\n" +
" var progress = timestamp - start;\n" +
" if (progress < 2000) {\n" +
" window.requestAnimationFrame(step);\n" +
" } else {\n" +
" window.close();\n" +
" }\n" +
"}\n" +
"\n" +
"window.requestAnimationFrame(step);");
还有windowHandler有的关了有的没关
我不知道如何通过 javascript
关闭 firefox 中的标签页
要求是10秒后直接关闭tab,或者页面加载完成后关闭tab
我不确定我是否完全理解这个问题,但您似乎正在尝试打开一个新选项卡,运行 一些代码,然后在打开 10 秒后关闭?
我已经在 firefox 中对此进行了测试,它适用于多个选项卡,并且每个选项卡都会自行关闭。
((JavascriptExecutor) webDriver).executeScript(
"function openAndClose(url, timeout){" +
" var tab = window.open(url);" +
" setTimeout(function(){tab.close()},timeout);" +
"}" +
"openAndClose('https://www.google.com', 3000);");
如果不是这样,你能否更清楚地解释你的问题。
我使用多线程方式打开选项卡,当页面完成加载时关闭选项卡
这是我的打开标签代码
((JavascriptExecutor)webDriver).executeScript("window.open('"+url+"')");
关闭选项卡时,我迭代 webDriver.getWindowHandles()
并使用函数 executeScript
,但发现一些错误,这是我的代码
此代码在 chrome 中工作正常,它关闭所有选项卡,但在 firefox 中一些 windowHandler 被关闭而一些没有
((JavascriptExecutor) webDriver).executeScript("setTimeout(function(){window.close()},10000)");
或
((JavascriptExecutor) webDriver).executeScript("setInterval(function(){window.close()},10000)");
我尝试使用这段代码,但它也像上面的代码
((JavascriptExecutor) webDriver).executeScript("var script = document.createElement('script');\n" +
"script.type = 'text/javascript';\n" +
"script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" +
"script.innerHTML = 'window.close()';\n" +
"document.body.appendChild(script);");
但是当我删除 "script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" +
时只使用 window.close()
它可以关闭所有选项卡,但不符合需要
所以我认为可能 setTimeout
和 setInterval
函数在 firefox
然后我将代码更改为
((JavascriptExecutor) webDriver).executeScript("var start = null;\n" +
"\n" +
"function step(timestamp) {\n" +
" if (!start) start = timestamp;\n" +
" var progress = timestamp - start;\n" +
" if (progress < 2000) {\n" +
" window.requestAnimationFrame(step);\n" +
" } else {\n" +
" window.close();\n" +
" }\n" +
"}\n" +
"\n" +
"window.requestAnimationFrame(step);");
还有windowHandler有的关了有的没关
我不知道如何通过 javascript
关闭 firefox 中的标签页要求是10秒后直接关闭tab,或者页面加载完成后关闭tab
我不确定我是否完全理解这个问题,但您似乎正在尝试打开一个新选项卡,运行 一些代码,然后在打开 10 秒后关闭?
我已经在 firefox 中对此进行了测试,它适用于多个选项卡,并且每个选项卡都会自行关闭。
((JavascriptExecutor) webDriver).executeScript(
"function openAndClose(url, timeout){" +
" var tab = window.open(url);" +
" setTimeout(function(){tab.close()},timeout);" +
"}" +
"openAndClose('https://www.google.com', 3000);");
如果不是这样,你能否更清楚地解释你的问题。