简体   繁体   中英

In Selenium Java client what is the difference between addArguments and setPreference methods?

When using Java and Selenium, frequently when I google how to change a certain behaviour I find some example that explains how to change a setting using either addArgument or set_preferences, something like this:

    options.addArguments("dom.webdriver.enabled=False");
    options.addArguments("useAutomationExtension=False");
    
    profile.setPreference("dom.webdriver.enabled", False);
    profile.setPreference("useAutomationExtension", False);        

Looking at the Javadoc isn't very helful:

addArguments Javadoc

设置偏好 Javadoc

Could someone explain the relation between these? Any conceptual differences? They seem very overlapping.

Furthermore, there is desiredCapabilities and capabilities that also seems very similar. Why do these exist and how do they relate to each other?

https://www.chromium.org/administrators/configuring-other-preferences

preferences are settings that are used to control the browser behavior, full list of supported preferences could be found at:

https://chromium.googlesource.com/chromium/src/+/7e762c1f17514a29834506860961ba2f24e7e6e5/components/content_settings/core/common/pref_names.cc

https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc

arguments are commands that can be passed as command line arguments to the chrome binnary eg

chrome.exe --headless

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/chromium/ChromiumOptions.html#addArguments(java.lang.String...)

full list: https://peter.sh/experiments/chromium-command-line-switches/

For firefox:

Arguments: https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options

Add all settings within the firefox config (which you can access by typing about:config in the address bar) can be set using options.addPreference

addArguments()

addArguments() is the method to configure the AbstractDriverOptions to manage the firefox specific settings in a way that geckodriver can understand using an instance of FirefoxOptions Class.

As an example:

FirefoxOptions option=new FirefoxOptions();
options.addArguments("start-maximized");

setPreference()

setPreference() is the method to configure the firefox profile specific settings in a way that geckodriver can understand using an instance of FirefoxProfile Class.

As an example:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "/home/ranjith/Downloads");
profile.setPreference("browser.download.folderList", 2);

References

You can find a couple of relevant detailed discussions in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM