簡體   English   中英

在 Selenium Java 客戶端中 addArguments 和 setPreference 方法有什么區別?

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

當使用 Java 和 Selenium 時,經常當我在谷歌上搜索如何更改某個行為時,我會找到一些示例來解釋如何使用 addArgument 或 set_preferences 更改設置,如下所示:

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

查看 Javadoc 並不是很有幫助:

addArguments Javadoc

設置偏好 Javadoc

有人可以解釋這些之間的關系嗎? 有什么概念上的區別嗎? 它們看起來非常重疊。

此外, desiredCapabilities的能力和capabilities看起來也非常相似。 為什么這些存在以及它們如何相互關聯?

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

首選項是用於控制瀏覽器行為的設置,支持的首選項的完整列表可以在以下位置找到:

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 是可以作為命令行 arguments 傳遞到 chrome 二進制文件的命令,例如

chrome.exe --headless

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

完整列表: https://peter.sh/experiments/chromium-command-line-switches/

對於 firefox:

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

在 firefox 配置中添加所有設置(您可以通過在地址欄中鍵入 about:config 來訪問)可以使用 options.addPreference 進行設置

添加參數()

addArguments()是配置 AbstractDriverOptions 以管理 firefox 特定設置的方法,geckodriver 可以使用FirefoxOptions Class 的實例來理解。

舉個例子:

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

設置偏好()

setPreference()是使用 FirefoxProfile Class 實例以geckodriver可以理解的方式配置 firefox 配置文件特定設置的方法。

舉個例子:

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

參考

您可以在以下位置找到一些相關的詳細討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM