[英]How to disable geolocation permissions for a website using firefox capabilities in selenium webdriver
[英]How to enable or disable geolocation by selenium test case
我想通過單擊該彈出窗口的允許按鈕允許/阻止站點訪問我的當前位置,我的 chrome 版本是62.0 ,chrome 驅動程序版本是3.6.0 ,我使用的是 ubuntu 16.04 ,我的代碼片段是,
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addArguments("start-maximized");
options.addArguments("--disable-geolocation");
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
driver = new ChromeDriver(capabilities);
但這不起作用,有人可以建議我完美的解決方案嗎?
Robot r = new Robot();
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
為此,請使用java.awt.Robot
類,首先進行手動測試,然后可以根據需要更改密鑰處理程序。
對於那些在沒有機器人的情況下尋找方法的人,您可以使用選項來做到這一點:拒絕將 profile.default_content_setting_values.geolocation 設置為 1,自動接受設置為 2(這似乎是當前的默認值)
ChromeOptions options = new ChromeOptions();
ArrayList<String> opArgList = new ArrayList<>(); // using an array list (so we can extend it with other passed in options)
opArgList.add("--no-sandbox");
opArgList.add("--disable-dev-shm-usage");
String[] opArg = opArgList.toArray(new String[0]);
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.geolocation", 1);
options.setExperimentalOption("prefs", prefs);
options.addArguments(opArg);
driver = new RemoteWebDriver(new URL("http://localhost:4444/"), options);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.