如何通过硒调用JS文件 [英] How to call JS file through selenium

查看:151
本文介绍了如何通过硒调用JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以通过selenium Web驱动运行我的JS文件?

我在我的firefox浏览器中禁用和启用JS脚本正在跑步。有没有可能做到这一点?



目前,我在Firefox配置文件中设置首选项,并启动浏览器来禁用JavaScript,有可能在运行时?

解决方案

为什么要禁用Javascript?您需要启用它们才能执行您自己的脚本。您要使用 JavascriptExecutor
$ b

 字符串fileContents = Files.toString(new File(c:\\ fullpathtoyourJS\\ \\\test.js),Charsets.UTF_8); 
JavascriptExecutor js =(JavascriptExecutor)驱动程序;
js.executeScript(fileContents);


Is there a way I can run my JS file through selenium Web driver?

I'm given to disable and enable JS script in my firefox browser when my tests are running. Is it possible to do that?

Currently, I set preferences in firefox profile and launch the browser to disable javascript, it is possible to do at runtime?

解决方案

Why are you disabling the Javascript? You need to enable them in order to execute your own script. You want to use JavascriptExecutor. You can do something like below.

String fileContents = Files.toString(new File("c:\\fullpathtoyourJS\\test.js"), Charsets.UTF_8);
JavascriptExecutor js = (JavascriptExecutor)driver;         
js.executeScript(fileContents); 

这篇关于如何通过硒调用JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆