I download ChromeDriver and by defaults the browser language is in English, I need to change it to Spanish, and I have been unable.
public WebDriver getDriver(String locale){
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
return new ChromeDriver();
}
public void initializeSelenium() throws Exception{
driver = getDriver("en-us")
}
You can do it by adding Chrome's command line switches "--lang".
Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es, see API for details.
The following is a working example of C# code for how to start Chrome in Spanish using Selenium.
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);
Java code should be pretty much the same (untested). Remember, locale here is in the form language[-country] where language is the 2 letter code from ISO-639.
public WebDriver getDriver(String locale){
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + locale);
return new ChromeDriver(options);
}
public void initializeSelenium() throws Exception{
driver = getDriver("es"); // two letters to represent the locale, or two letters + country
}
For me, --lang didn't work. It seems to set the language of the first opened tab, all others chrome processes are started with --lang=en-US.
What did work is the following:
DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("intl.accept_languages", language);
options.setExperimentalOption("prefs", prefs);
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
I had problems with Chrome using US date format (mm/dd/yyyy) instead of the GB dd/mm/yyyy format (even though I had set these in Chrome). Using:
options.addArguments("--lang=en-GB");
resolved this.
As of now (Jan 2020 - Chrome Version 79.0.3945.130) The C# in the accepted answer does not work.
The simplest approach that I can find to work in C# presently:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("intl.accept_languages", language);
WebDriver driver = new ChromeDriver(chromeOptions);
I was trying the same and above listed nothing worked for me, at last I tried the below and it worked:
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("intl.accept_languages", "ja-jp,ja");
chromeOptions.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(chromeOptions);
For me --lang also didn't work. I wanted to perform Facebook Login tests with specific language (en-US instead of en-GB) and what I found is that some pages (like Facebook) set interface according to system environment variable LANG... So if above answers doesn't work, try changing LANG environment variable. Tested on Linux.
I had the same problem. I tried to solve the problem including
chromeOptions = Options()
chromeOptions.add_argument('--lang=es')
but it didn't work (I have discovered that isn't necessary).
It works when I changed the locale:
locale -a
sudo apt-get install language-pack-es
sudo dpkg-reconfigure locales
It is es_ES.UTF-8 UTF-8 for Spanish. Finally, you have to start a new shell in order to get new env variables (LANG=C.UTF-8 to es_ES.UTF-8)
For latest versions below code should work.
ChromeOptions options = new ChromeOptions();
options.addArguments("--accept-lang=" + locale);
return new ChromeDriver(options);
https://peter.sh/experiments/chromium-command-line-switches/#accept-lang
For people using Selenium with ruby:
I made it work this way:
prefs_hash = {
'credentials_enable_service' => false,
'profile' => {
'password_manager_enabled' => false,
},
'intl.accept_languages' => 'fr-FR', // <- here
}
// [...]
browser = Watir::Browser.new :chrome, :prefs => prefs_hash, switches: switches, desired_capabilities: caps
website = 'https://e-katalog.lkpp.go.id/id/search-produk?q=' + mylist[i] +
'&order=relevance&limit=12&offset=' + str(1)
chrome_options = Options()
chrome_options.add_argument("--lang=en");
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {
"translate_whitelists": {'id':'en'},
"translate":{"enabled":"True"}}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe',options=chrome_options)
time.sleep(10)
driver.get(website)
time.sleep(5)
Related
Here is my current code to launch browser without any proxy:
properties = getGridProperties();
DesiredCapabilities capabilities = null;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("layout.css.devPixelsPerPx","0.9");
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
options.addPreference("dom.webnotifications.enabled", false);
if (properties.containsKey("hub.gecko.driver.path"))
System.setProperty("webdriver.gecko.driver", properties.getProperty("hub.gecko.driver.path"));
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
if (browserType.equalsIgnoreCase("oldfirefox")) {
capabilities.setCapability("marionette", false);
capabilities.setCapability("platform", "LINUX");
options.merge(capabilities);
}
printInfo("initRemoteWebDriver:started:" + System.currentTimeMillis());
capabilities.setCapability("idleTimeout", 150);
String nodeURL = "http://" + properties.getProperty("grid.hub.host") + "/wd/hub";
capabilities.setCapability("idleTimeout", 150);
capabilities.setCapability("name", this.getClass().getCanonicalName());
driver = new RemoteWebDriver(new URL(nodeURL), capabilities);
setDriver(driver);
getDriver().manage().window().maximize();
printInfo("****Firefox browser launched***");
printInfo("initRemoteWebDriver:finished:" + System.currentTimeMillis());
Desired proxy details to be set:
HTTP_PROXY = 'http://www-proxy.us.abc.com:80'
HTTPS_PROXY = 'http://www-proxy.us.abc.com:80'
What is the simplest way to do this without changing the current code too much?
Hi please try this one
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://www-proxy.us.abc.com:80");
capabilities.setCapability(CapabilityType.PROXY, proxy);
I hope it will work for you.
Thanks
Please refer Selenium documentation - https://www.selenium.dev/documentation/en/webdriver/http_proxies/
An alternative would be to use JVM flags. That way you don't have to change your code at all.
java -Dhttp.proxyHost=http://www-proxy.us.abc.com -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=”localhost|127.0.0.1|10.*.*.*”
I am new in selenium. I need a browser without a graphical interface because the project will start with Jenkins. I decided to use ChromeDriver in Headdless mode.
When I use ChrimeDriver in normal mode, I can click on all elements:
WebDriver driver = new ChromeDriver();
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click(); // is ok
}
But when I use Headdless mode then I have: ElementNotVisibleException: element not visible. What could be wrong? Thank you for every clue.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click();//ElementNotVisibleException dont see next li elements
//div[#id='accordian']/div/ul/li
}
While working with Selenium Client v3.11.0, Chrome Driver v2.36 and Chrome Browser v65.x in Headless Mode, you need to pass the following arguments through an instance of ChromeOptions Class while initializing the WebDriver and the Web Browser as follows :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in");
You need to pass "--headless", chrome option like below.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
For entire list of chrome options, refer the following URL. It explains every command line switches in detail.
https://peter.sh/experiments/chromium-command-line-switches/
While working with headless mode, I encountered org.openqa.selenium.UnhandledAlertException due to not handling popping out of Alert Boxes. So it is better if you could handle the alert boxes.
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
I'm doing Selenium JAVA tests for mobile using the Chrome driver with its emulator. The problem is that I can't use the most advanced mobile devices like iPhone 7,8 etc even-though its in my drop-down list when manually testing in the devtools.
This is the driver init. that works perfectly with many mobile devices:
if(browser.equalsIgnoreCase("mobileIPhone6")){
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "iPhone 6");
String exePathChromeDriver = Consts.chromeDriverPath;
System.setProperty("webdriver.chrome.driver", exePathChromeDriver);
PropertyLoader.loadCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);
BUT, When I change line 3 to "iPhone 7" I'm getting this error:
2018-03-16 21:25:49 INFO LogLog4j:210 - org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: chromeOptions
from unknown error: cannot parse mobileEmulation
from unknown error: 'iPhone 7' must be a valid device
from unknown error: must be a valid device
Any idea why? many thanks
I've found the solution and it is now working properly. I had to set the Device Metrics object first:
...else if(browser.equalsIgnoreCase("iPhone678")){
String exePathChromeDriver = Consts.chromeDriverPath;
System.setProperty("webdriver.chrome.driver", exePathChromeDriver);
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 375);
deviceMetrics.put("height", 667);
deviceMetrics.put("pixelRatio", 2.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions); }
Why not use Dimension class?
Define an enum EmulatedDevices and use following style:
driver.manage().window().setSize(new Dimension(EmulatedDevices.IPHONE7.getWidth(),EmulatedDevices.IPHONE7.getHeight()));
This should work:
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "iPhone 6");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.uk");
I'm a bit new to selenium and I was looking to use it to run various automated tasks through Tor. I'm on Windows 10 using Java through Eclipse with the Selenium packages as referenced libraries. I've checked out several other tutorials and forum threads detailing how to set up Tor in selenium. I started out trying to use the following code:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
WebDriver driver = new FirefoxDriver(binary, torProfile);
driver.get("https://www.google.com");
Using that code produces this error:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
I tried referencing this thread for a solution. I then used the following code based off of that thread:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
torProfile.setPreference("webdriver.load.strategy", "unstable");
try
{
binary.startProfile(torProfile, new File(profilePath), "");
}
catch (IOException e)
{
e.printStackTrace();
}
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
WebDriver driver = new FirefoxDriver(binary, profile);
driver.get("https://www.google.com");
Using that code launches Tor for me; however, I still receive the same error as before and the driver cannot control that Tor window.
I would appreciate any help or advice.
While using IE for automation using Selenium Webdriver, I am able to open the URL but finding the element on that page is throwing the following exception:
org.openqa.selenium.NoSuchWindowException: Unable to find element on
closed window (WARNING: The server did not provide any stacktrace
information)
I have tried the driver.switchTo.window() method but it's not working.
I have searched it for hours and I am not getting anywhere.
Here's the code:
public static Selenium selenium;
public static void main(String args[]) {
try {
System.setProperty(
"webdriver.ie.driver",
"D:\\Driver\\IEDriverServer_Win32_2.32.3_latest\\IEDriverServer.exe");
DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
capab.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
WebDriver driver = new InternetExplorerDriver(capab);
driver.get("http://www.google.com");
driver.findElement(By.xpath(".//*[#id='addlang']/a[1]")).click();
} catch (Exception e) {
e.printStackTrace();
}
}
Remove capability INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS and manually set your IE protected mode settings to be the same for all zones.
Source:
http://jimevansmusic.blogspot.com/2012/08/youre-doing-it-wrong-protected-mode-and.html
NoSuchElementException is occurred during implementation of InternetExplorerDriver in Selenium WebDriver
case "ie_driver":
//IE CODE
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "https://testvmm6.partnersonline.com/vmm");
cap.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"//exe//IEDriverServer1.exe");
cap.setCapability("IE.binary", "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setJavascriptEnabled(true);
cap.setCapability("requireWindowFocus", true);
cap.setCapability("enablePersistentHover", false);
The issue that helped me was to set init page (IE 11 both 32 and 64)
private WebDriver getIEDriver() {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, INIT_PAGE);
File file = new File("E:/drivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
return new InternetExplorerDriver(cap);
}
The best bet here is to make some tweaks to the registry:
Go to registry edit (regedit from windows run)
Look in your registry under the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Zones. Over there, you should see keys number 0-4 . Under these keys 0-4, look for a value named 2500
For all the keys from 0-4, have the same data for value 2500. For example, for key 0 if the value 2500 has data as 3 (hex data), then make the data for value 2500 as 3 for all the other keys (1,2,3,4).
Now try to run the script.