我使用 Selenium 和 ChromeDriver 在我的网站上运行测试。我在
localhost
中运行它们。我的一些测试会激活页面上使用 clipboard
api 的元素。这会弹出一个请求许可的框。我需要以某种方式绕过此框并允许浏览器访问剪贴板而无需用户干预。我尝试了网上能找到的每一个选项,但它们都被忽略了。除了最后两行,它们不会被忽略,但会导致错误。
这是我所有的(到目前为止不成功的)尝试:
var opts = new ChromeOptions();
opts.AddArgument("start-maximized");
//opts.AddUserProfilePreference("profile.default_content_settings.popups", 0);
//opts.AddUserProfilePreference("profile.default_content_settings.notifications", 1);
//opts.AddUserProfilePreference("profile.default_content_setting_values.notifications", 1);
//opts.AddUserProfilePreference("profile.default_content_setting_values.clipboard-read", 1);
//opts.AddUserProfilePreference("profile.default_content_setting_values.clipboard-write", 1);
//opts.AddUserProfilePreference("profile.content_settings.exceptions.clipboard", "*={'setting': 1}");
var chrm = new ChromeDriver("D:\\Projects\\General", opts);
//chrm.SetPermission("clipboard-read", "granted");
//chrm.SetPermission("clipboard-write", "granted");
driver = chrm;
我使用
SetPermission
得到的错误是:
OneTimeSetUp:OpenQA.Selenium.WebDriverArgumentException:无效 论点:不能向不透明的来源授予权限。 (会议 信息:chrome=119.0.6045.105)
根据我的理解,
localhost
是一个不透明的起源。
如何绕过此访问警报?
提前致谢
试试这个代码:
var clipboardException = new Dictionary<string, object> {{"[*.],*",new
Dictionary<string, object> {{"last_modified",
DateTimeOffset.Now.ToUnixTimeMilliseconds()},{"setting", 1}}}};
var opts = new ChromeOptions();
opts.AddArgument("start-maximized");
opts.AddUserProfilePreference("profile.content_settings.exceptions.clipboard", clipboardException);
var chrm = new ChromeDriver("D:\\Projects\\General", opts);