如何在Selenium远程WebDriver中禁用Flash

9

当使用Selenium Remote WebDriver时,如何禁用Flash对象的加载。 如果能为普通的webdriver提供解决方案将非常有帮助。

由于在大多数情况下,Flash对象是由JavaScript加载的, 我已尝试在webdriver和remote webdriver上禁用JavaScript,但并未奏效。

我尝试通过以下方式禁用JavaScript:

WebDriver driver = new FirefoxDriver();
((DesiredCapabilities) driver.getCapabilities()).setJavascriptEnabled(false);

我也尝试了以下方法:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(false);
WebDriver driver = new FireFoxDriver(caps);

我尝试了远程 WebDriver:

final DesiredCapabilities firefoxCapability = DesiredCapabilities.firefox();
firefoxCapability.setJavascriptEnabled(false);
new RemoteWebDriver(new URL("http://" + windowsIP + ":4444/wd/hub"), firefoxCapability);

执行以上语句后,远程服务器会显示:
Executing: [new session: <platform=ANY, javascriptEnabled=false, browserName=firefox, version=>] at URL:/session>

但是仍然所有的Javascript都在驱动程序加载的页面上执行,Flash也在加载。

请帮助我: 1. 如何停止Flash加载。 2. 需要在远程驱动程序上使用,因为我需要在IE、Firefox和Chrome上测试页面。因此加载Firefox配置文件将无法工作。

感谢您的帮助。


你找到答案了吗? - Tasawer Khan
你一定要接受 @TasawerKhan 提供的答案。 - alecxe
3个回答

4

我在Linux Mint上使用了以下代码,它可以正常工作:

FirefoxProfile profile= new FirefoxProfile();
profile.setPreference("plugin.state.flash", 0);
FirefoxDriver driver = new FirefoxDriver(profile);

0

虽然这个问题已经在不同的论坛上得到回答了...所以我会为您整理一下...

我不确定Flash对象是否由JavaScript加载...但如果禁用JavaScript是一个问题,那么...

永远不要禁用Firefox驱动程序的Javascript,如果您想使用已禁用的驱动程序,请尝试使用专门针对非JavaScript页面的HTMLUNITDRIVER。

原因是Firefox驱动程序的重要部分都是用JavaScript实现的,禁用会带来严重的问题。

另一方面,HtmlUnitDriver是自动化测试的最快和最佳方法(特别是针对没有JS的页面)。

请查看此群组讨论https://groups.google.com/forum/?fromgroups=#!topic/webdriver/daLOzCiU_h4%5B1-25%5D


谢谢您的回答。<br/>1. 我遇到了许多使用JavaScript从URL加载Flash电影的页面,因此要停止Flash加载,我需要关闭JavaScript。<br/>2. 使用HTMLunit无法在不同的浏览器(Firefox、Chrome、IE)上测试页面,因此我正在使用远程Web驱动程序。 - Panshul
欢迎:)。HTMLUnitDriver本身就像FF、IE和Chrome一样是一个不同的浏览器,但是它是用Java实现的。这将给您结果,但不是准确的结果。_我知道这不是一个更好的解决方案,但对于Firefox,您可以手动创建禁用Flash的配置文件(因此阻止Flash),然后将相同的配置文件重复使用于多个远程驱动程序实例中_。 - Anuragh27crony

0

我也遇到了同样的问题,需要在Chrome中解决。这是我让它正常工作的方法:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-bundled-ppapi-flash");
    WebDriver webDriver = new org.openqa.selenium.chrome.ChromeDriver(options);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接