如何从 Selenium 内部调用 JavaScript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27057541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:39:44 来源:igfitidea点击:
How to call a JavaScript function from within Selenium?
提问by bpk
I need to call a JavaScript function from Selenium WebDriver in Firefox. I use this command in Firebug's Command Editor to invoke a file upload application after logged into my website:
我需要在 Firefox 中从 Selenium WebDriver 调用 JavaScript 函数。我在 Firebug 的命令编辑器中使用此命令在登录我的网站后调用文件上传应用程序:
infoPanel.applicationManager.changeApp('FileUploader', {action: 'new'})
Is there a way to execute this from Selenium?
有没有办法从 Selenium 执行这个?
采纳答案by Helping Hands
WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor)
{
((JavascriptExecutor)driver).executeScript("yourScript();");
}
回答by Sergii Tanchenko
Try this:
尝试这个:
WebDriver driver = new ChromeDriver();
((JavascriptExecutor)driver).executeScript("yourScript();");

