How to add allow site on location of chrome content setting with selenium - selenium

I want to add site "a.com" on mobile chrome using selenium.
Option - [Advanced-content setting-location-ask before accessing-allow site]
Because i want to rid of the popup on my testing
Does anyone know?

To disable chrome asking for location you need to use ChromeOptions and disable geolocation from profile settings
ChromeOptions options = new ChromeOptions();
JSONObject jsonObject = new JSONObject();
jsonObject.put("profile.default_content_settings.geolocation", 2);
options.setExperimentalOption("prefs", jsonObject);
WebDriver driver = new ChromeDriver(options);
Please see that the whole answer is already explained in this SO post.
Edit : In case, the option is to be kept enabled, you just need to change this line
jsonObject.put("profile.default_content_settings.geolocation", 2);
to
jsonObject.put("profile.default_content_settings.geolocation", 1);

Use chrome devtools protocol can do this.Browser.grantPermission allow to config the geolocation permission before accessing the target website. You can look at my another answer for more detail Setting sensors (location) in headless Chrome
.

The below code snippet working for me to disable that pop up. Instead of profile.default_content_settings.geolocation,
I used profile.default_content_setting_values.notifications.
ChromeOptions options = new ChromeOptions();
JSONObject jsonObject = new JSONObject();
jsonObject.put("profile.default_content_setting_values.notifications", 1);
options.setExperimentalOption("prefs", jsonObject);
WebDriver driver = new ChromeDriver(options);
or
DesiredCapabilities caps = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("profile.default_content_setting_values.notifications", 1);
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);

Related

How to enable save passwords using selenium chromedriver

when I start chromedriver by default password save options are disabled (I can see options to save password are enabled in chrome config), but when I log in any website, chromedriver never ask me to save the password.
For my tests I need to save passwords, I have tried some options but nothing works.
I used same profile to open chromedriver, so I don't use the temporary profiles that chromedriver creates every time I launch it.
(sorry about my english)
Selenium Java
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", true);
prefs.put("profile.password_manager_enabled", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(options);
Selenium Python
var options = new ChromeOptions();
options.AddUserProfilePreference("credentials_enable_service", true);
options.AddUserProfilePreference("profile.password_manager_enabled", true);
var driver = new ChromeDriver(options);

How to disable dns over https in selenium

I am writing test using selenium, java, chrome.
How can I turn off "dns over https" in chrome settings?
I need it because my intranet DNS have different data than internet ones.
I've tried to add following options.
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("ignore-certificate-errors");
options.addArguments("--disable-async-dns");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-web-security");
ChromeDriver driver = new ChromeDriver(options);
It didn't help.
I've even tried to change options by clicking in settings
driver.get("chrome://settings/security");
String disableDNSOverHttpsButton = "/html/body/settings-ui//div[2]/settings-main//settings-basic-page//div[1]/settings-section[4]/settings-privacy-page//settings-animated-pages/settings-subpage/settings-security-page//settings-secure-dns//settings-toggle-button//div/cr-toggle";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(disableDNSOverHttpsButton)));
driver.findElement(By.xpath(disableDNSOverHttpsButton)).click();
There is response "org.openqa.selenium.NoSuchElementException: "
local_state = {
"dns_over_https.mode": "off",
"dns_over_https.templates": "",
}
options.add_experimental_option("localState", local_state)
The solution is to use setExperimentalOption.
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
Map<String, Object> localState = new HashMap<String, Object>();
localState.put("dns_over_https.mode", "off");
localState.put("dns_over_https.templates", "");
options.setExperimentalOption("localState", localState) ;
ChromeDriver driver = new ChromeDriver(options);

Selenium newer Chrome cannot disable browser notification (Tried other solns)

I know this is an old question, and I've tried answers from a few posts such as Disable Chrome notifications (Selenium)
Unfortunately none worked, the browser notification popup still comes and interrupts my simulations.
My Chrome version is 75.0.3770.100 (Official Build) (64-bit), running on MacOS.
Edit:
After this question was marked as a duplicate of How to disable push-notifications using Selenium for Firefox and Chrome?, I've tried the solutions, but it still did not work for me.
String chromePath = "somepath/selenium-2.48.2/chromedriver";
System.setProperty("webdriver.chrome.driver", chromePath);
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
Below are the original solutions I tried.
String chromePath = "somepathto/chromedriver";
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", chromePath);
WebDriver driver = new ChromeDriver(chromeOptions);
// Login
try {
driver.get(sometestURL);
driver.manage().window().maximize();
// do some operations
} catch (Exception ex) {
System.out.println(ex);
}
I also tried this:
String chromePath = "somepath/selenium-2.48.2/chromedriver";
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.driver", chromePath);
WebDriver driver = new ChromeDriver(chromeOptions);
But the notification still comes "xxxxwebsite wants to show notifications Allow Block" on the upper left of the window.
What I did not do right?
I just tested this on Windows with Chrome 75.0.3770.142 (64 bit) and ChromeDriver 75.0.3770.90 (see log below) against web site https://www.nzz.ch (because it asks for permission to show push notifications). I used Selenium 3.14.0.
08:27:37.184 [main] INFO i.g.bonigarcia.wdm.WebDriverManager - Exporting webdriver.chrome.driver as C:\Users\xxxxx\.m2\repository\webdriver\chromedriver\win32\75.0.3770.90\chromedriver.exe
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770#{#1003}) on port 32864
For me the switch also mentioned here and in the linked resources reliably deactivates the popup from being displayed:
options.addArguments("--disable-notifications");
Changing the default setting profile.default_content_setting_values.notifications is not necessary in my case. So if this does not work for you
either you are not setting the option correctly,
you are using a (possibly outdated) ChromeDriver version not supporting it or
for some reason on MacOS the driver does not implement it.
P.S.: In both Firefox and Chrome for me it was not problem to e.g. click links and navigate to other pages even during the notification pop-up was shown. Did you have any problems there or is the pop-up just an annoyance?

Unable to find element on new tab link

When I run a browser with extension a pop up arise that says "Disable developer mode" to do so my script clicks on Disable, it opens a new tab with url "chrome://extensions/"
Now to click on checkbox of developer mode it always give an error "Unable to locate element."
driver.findelement(By.id("toggle-dev-on"));
http://prntscr.com/f8fbde
Here is the solution for your Question:
As per best practices to work with Selenium 3.4.0 you must download the latest chromedriver v2.29 from here, update your Google Chrome to 58.x.
Updating your chromedriver to v2.29 will solve your issue of Disable developer mode
To work with Google Chrome you can take help of ChromeOptions Class as follows:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://gmail.com");
Let me know if this solves your Question.
You need to modify the appropriate browser profile to have JS disabled. Like for FireFox/Chrome you can tell Selenium which profile to use.
Chrome :
Map prefs = new HashMap();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
FireFox :
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("dom.webnotifications.enabled", false);
WebDriver driver = new FirefoxDriver(ffprofile);
Hope this will work out for your case.

Selenium:Google Chrome Options and capabilities

I just wanted to know is it possible to set homepage of Chrome using capabilities and Chrome options in Selenium.
Yes it is possible to instantiate ChromeDriver with using both DesiredCapabilities and ChromeOption to set your desired Homepage as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put( "browser.startup.homepage", "http://my.home.page" );
preferences.put( "browser.startup.page", START_WITH_HOME_PAGE );
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
capabilities.setCapability( ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
Just to be clear, the capabilities you are trying to set here are options that you use to customize and configure a ChromeDriver session.
Below are the keys which you can use to set for that session:
"browser.startup.homepage", "startup.homepage_welcome_url", "startup.homepage_welcome_url.additional" etc.
You can pass URL for these or if you don't want you can also set something like : "about:blank" as value