java FirefoxDriver:如何禁用 javascript、css 并立即设置 sendKeys 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3526361/
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-10-30 02:15:36  来源:igfitidea点击:

FirefoxDriver: how to disable javascript,css and make sendKeys type instantly?

javawebdriver

提问by KJW

While using FirefoxDriver to write tests,

在使用 FirefoxDriver 编写测试时,

I discovered loading of pages are really slow because of javascript and css being executed. IS there anyway to disable this ? possible to even install Noscript plugin to profile ?

我发现页面加载真的很慢,因为正在执行 javascript 和 css。无论如何要禁用它吗?甚至可以安装 Noscript 插件来配置文件?

additionally, sendKeys(), actually types out the text. however, this is quite slow for long text, anyway to instantly type all the string int othe input box ?

此外,sendKeys() 实际上是输入文本。然而,这对于长文本来说很慢,无论如何要立即在输入框内输入所有字符串?

回答by Sergii Pozharov

You can disable javaScript in FirefoxProfile:

您可以在FirefoxProfile以下位置禁用 javaScript :

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);

I do not think that there's a way to disable CSS and this not what you should do - this may break your web application, and disabling JavaScript may do this too.

我不认为有一种方法可以禁用 CSS,这不是您应该做的 - 这可能会破坏您的 Web 应用程序,而禁用 JavaScript 也可能会这样做。

There's no way to set the value of the text field directly - WebDriver is designed to simulate the real user "driving" the browser - that's why there's only sendKeys.

没有办法直接设置文本字段的值——WebDriver 旨在模拟真实用户“驱动”浏览器——这就是为什么只有 sendKeys。

However you can set the value of the element via JavaScript call (if you will not disable it, of course). This is faster for the long test, but this is not the emulation of the user interaction so some validations may not be triggered, so use with caution:

但是,您可以通过 JavaScript 调用设置元素的值(当然,如果您不禁用它)。这对于长时间的测试来说更快,但这不是用户交互的模拟,因此可能不会触发某些验证,因此请谨慎使用:

private void setValue(WebElement element, String value) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].value = arguments[1]", element, value);
}

and use it:

并使用它:

WebElement inputField = driver.findElement(By...);
setValue(inputField, "The long long long long long long long text......");

回答by marie

See also Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python

另请参阅在使用 Python 的 Selenium WebDriver 测试中,不希望在 Firefox 上加载图像和渲染 CSS

To hide CSS and images:

隐藏 CSS 和图像:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.stylesheet", 2);
profile.setPreference("permissions.default.image", 2);
FirefoxDriver browser = new FirefoxDriver(profile);

回答by Ricardo LR

Also you can use PhantomJSis WebKit browser without User Interface so it is really faster than FireFox or Chrome. There is web driver support for PhantomJS.

你也可以使用PhantomJS是没有用户界面的 WebKit 浏览器,所以它真的比 FireFox 或 Chrome 快。PhantomJS 有 Web 驱动程序支持。