How to set chrome browser language using java + selenium? - java

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);
}

Related

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");

Selenium can't find WebElements when trying to use headless chromedriver option

I am running selenium with chromedriver and it all works fine.
Lately I tried to work with 10 chromedrivers simultaneously and it takes all the memory available, so I tried to solve it with headless option in ChromeOptions.
Tried those options:
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
But when the program runs with --headless option it won't work. (it worked before without the headless)
this is how I setup my chromeDriver:
private ChromeDriver setupChromeDriver(String proxyAddress, String downloadFilePath){
try{
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
if(!proxyAddress.equals(""))
cap.setCapability("proxy", setupChromeProxy(proxyAddress));
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().build();
options.merge(cap);
logger.info("Selenium chrome driver set up with updated download location");
return new ChromeDriver(chromeDriverService, options);
}
catch (Exception e){
logger.info(e.getMessage());
logger.info(BaseUtilities.getStackTrace(e));
e.printStackTrace();
return null;
}
}
example how I find element which works without the headless option:
driver.findElement(By.xpath("//input[#id='identifierId']")).sendKeys(dumper.getUsername() + Keys.ENTER);
I am using Windows 10, chrome Version 75.0.3770.142 (Official Build) (64-bit),
ChromeDriver 75.0.3770.140
Tried to find some information in similar posts but couldn't find a working solution.
Thank you.
You can simply try like this
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver wDriver = new ChromeDriver(chromeOptions);

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. :)

Can't connect my 'BrowserFactory' to 'Selenium Grid'

I can't connect my BrowserFactory to Selenium Grid. Any ideas why the following code won't work?
public static WebDriver getDriver() throws Exception {
try {
// Load the driver selected by user
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
p.load(fi);
if(p.getProperty("use_grid").equalsIgnoreCase("true")) {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.getBrowserName();
desiredCapabilities.setPlatform(Platform.WINDOWS);
return new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), desiredCapabilities);
}
String browserName = p.getProperty("browser");
switch (browserName) {
case "firefox":
if (null == webdriver) {
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
webdriver = new FirefoxDriver();
}
break;
I have the node and grid up and running successfully.
Thanks.
It looks like you're not setting the browser when using selenium grid. Try changing this line:
desiredCapabilities.getBrowserName();
to this:
desiredCapabilities.setBrowserName("firefox");
or this if you're properties are in the correct format and that code is working correctly:
desiredCapabilities.setBrowserName(p.getProperty("browser"));

Disable Chrome notifications (Selenium)

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();
}
}

Categories