Disable Chrome notifications (Selenium) - java

I just want to disable Chrome notifications in the Chrome opened by a Selenium Java application. (using java code)
Notifications like this one:
The problem is that settings manually set are lost after browser's window is closed.

you can use:
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(chrome_options=chrome_options)

This question was answered in the: "chromedriver-users" google forum.
This is the working answer:
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
driver = new ChromeDriver(ops);

Someone needs this for Capybara or Watir, you can pass the --disable-notifications as an argument like "--start-fullscreen", "--disable-infobars". The following workes:
Capybara.register_driver :chrome do |app|
args = ["--disable-notifications"]
Capybara::Selenium::Driver.new(app, {:browser => :chrome, :args => args})
end

public class MultipleWindowHandle
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "E:\\NEWSEL\\chromedriver.exe");
// Create object of HashMap Class as shown below.
Map<String, Object> prefs = new HashMap<String, Object>();
// Set the notification setting it will override the default setting.
prefs.put("profile.default_content_setting_values.notifications", 2);
// Create object of ChromeOption class.
ChromeOptions Roptions = new ChromeOptions();
// Set the experimental option.
Roptions.setExperimentalOption("prefs", prefs);
// Open chrome browser.
ChromeDriver driver = new ChromeDriver(Roptions);
driver.get("https://my.monsterindia.com/login.html");
Set<String> id = driver.getWindowHandles();
Object[] data = id.toArray();
driver.switchTo().window((String)data[1]); driver.close();
}
}

Related

The page I'm trying to access doesn't load when I use headless mode, but when I don't use it, it loads and performs the tests perfectly

I'm doing a test with selenium and java in eclipse, with chrome 62. And when I run it with headless mode enabled, the page I'm trying to access doesn't load. And if I don't activate it, yes it does.
Map<String, Object> prefs = new HashMap<String, Object>();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(BINARY_PATH);
WebDriverManager.chromedriver().browserVersion("62").driverVersion("2.35").cachePath(EXECUTION_PATH).setup();
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setHeadless(true);
prefs.put("profile.default_content_settings.popups", 0);
DriverFactory driverFactory = DriverFactory.getInstance();
driverFactory.setDownloadPath(
EXECUTION_PATH + Thread.currentThread().getName() + System.getProperty("file.separator"));
prefs.put("download.default_directory", driverFactory.getDownloadPath());
prefs.put("download.prompt_for_download", false);
chromeOptions.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(chromeOptions);

Unable to run the test scripts using Chrome Driver though am able to hit the browser with Driver.get("url")

My below code is giving expected result to hit chrome browser with the url and maximize the window.But after that it should go my my test scripts where is have login page and it is doing nothing over there
Please suggest
#BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\NCRExecutableWorkSpace\\automation-ncr\\Drivers\\chromedriver.exe");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", map);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://google.com");

How to set chrome browser language using java + selenium?

It does not work chrome browser language setting as below using selenium + Java. Can someone help on this to find reson behind that ?
ChromeOptions optionsChrome = new ChromeOptions();
optionsChrome.addArguments("--lang=ja");
driver = new ChromeDriver(optionsChrome);
I think you should call option with setExperimentalOption then add the language.
So it should be like:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--lang=ja");
I hope this will work for you.
This is my implementation for chrome / firefox
public WebDriver createWebDriver(BrowserType browserType) {
switch (browserType) {
case IE:
return new InternetExplorerDriver();
case CHROME:
if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver77.exe");
}
if (SystemUtils.IS_OS_LINUX) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver77");
}
return new ChromeDriver();
case FIREFOX:
if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/geckodriver.exe");
}
if (SystemUtils.IS_OS_LINUX) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/geckodriver");
}
return new FirefoxDriver();
default:
throw new RuntimeException("Unsupported browserType: " + browserType);
}

can i enable/disable image loading setting in selenium with the same driver object

final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
Map<String, Object> prefs = new LinkedHashMap<>();
prefs.put("profile.default_content_setting_values.images", 2);
chromeOptions.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.example.com/");
This code will stop the image loading. Now I want to re-enable the Image loading again with the same driver object ??
prefs.put("profile.default_content_setting_values.images", 0);
chromeOptions.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);
driver.get("https://connect.secure.wellsfargo.com/auth/login/present?origin=yodlee");
I am using this above code to changing the pref again to start loading the images again but with this code, new chrome is opening
driver = new ChromeDriver(capabilities)
but I want this with the same chrome driver.
My simple ask is to enable/disable image loading with the same driver object ?
it will be great if some body help me on this

How to download a file from a website in a specific folder, I am using Chorme

I'm trying to download a file from a website that has to be saved in a specific folder. Website http://bookboon.com/en/basics-of-accounting-information-processing-ebook
When I click on download it saves the file in the download section, I also tried to change the download directory in chrome settings, it doesn't work. I am trying automation (selenium, java). Is there any way?
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe");
d = new ChromeDriver();
d.get("http://bookboon.com/en/basics-of-accounting-information-processing-ebook");
d.findElement(By.id("email")).sendKeys("asd#ymail.com");
WebElement One=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
One.sendKeys("Studying");
One.sendKeys(Keys.TAB);
WebElement Two=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
Two.sendKeys("Engineer/Science MSc");
Two.sendKeys(Keys.TAB);
WebElement Three=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
Three.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
Three.sendKeys(Keys.TAB);
d.navigate().back();
downlinks = d.findElements(By.className("pdf"));
}
}
For Chromedriver It will work
String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
For Firefox: You need to setPreference
profile.setPreference("browser.download.dir", "Filepath");
Hope this helps. :)

Categories