Chromedriver: How to translate a page using selenium? - selenium

I need to translate a page from Japanese to English, using selenium in chrome browser. I tried different ways one of sample code snippet is as following
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Main {
private WebDriver driver=null;
WebDriverLoad a;
#Test
public void successfulDesignerLogin() throws Exception{
// final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// capabilities.setJavascriptEnabled(true);
String chromedriver = "/dev/Saved/chromedriver";
System.setProperty("webdriver.chrome.driver",chromedriver);
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=en-ca");
//Map<String, Object> prefs = new HashMap<String, Object>();
//prefs.put("intl.accept_languages", "en,en_US");
//options.setExperimentalOption("prefs", prefs);
ChromeDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.get("https://www.bbc.com/japanese");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.close();
}
}
I tried a couple of solution options.addArguments options.setExperimentalOption but nothing works can any one suggests me what can be the solution

You need to enable translate and add target language ID to the whitelist {"from" : "to"}.
"translate":{"enabled":"true"}
"translate_whitelists": {"ja":"en"}
in java:
Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> langs = new HashMap<String, Object>();
langs.put("ja", "en");
prefs.put("translate", "{'enabled' : true}");
prefs.put("translate_whitelists", langs);
options.setExperimentalOption("prefs", prefs);

Here is a C# version, directly add "translate" and "translate_whitelists" in "AddUserProfilePreference"
ChromeOptions options = new ChromeOptions();
Dictionary<string, object> LanguageList = new Dictionary<string, object>();
LanguageList.Add("fr", "en");
Dictionary<string, bool> enableObject = new Dictionary<string, bool>();
enableObj.Add("enabled", true);
options.AddUserProfilePreference("translate", enableObject);
options.AddUserProfilePreference("translate_whitelists", LanguageList);

Related

language translation in Selenium java

I am Trying to translate the webpage with following code but it did not work.
WebDriverManager.chromedriver().setup();
ChromeOptions option = new ChromeOptions();
option.addArguments("start-maximized");
option.addArguments("test-type");
option.addArguments("--lang=en-US");
option.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
option.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(option);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
driver.get("https://www.staffers.no/jobbsoker");
You can try with below code :
ChromeOptions option = new ChromeOptions();
option.addArguments("start-maximized");
option.addArguments("test-type");
option.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
option.setExperimentalOption("useAutomationExtension", true);
Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> langs = new HashMap<String, Object>();
langs.put("no", "en");
prefs.put("translate", "{'enabled' : true}");
prefs.put("translate_whitelists", langs);
option.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(option);
driver.get("https://www.staffers.no/jobbsoker");

Junit 5 tests are not running in parallel

I have created a simple program to intialte crome and one SYSO statement. I want to run the 2 #Test in parallel. But it is executing alwasy is seq. What could be teh reason for it to not recognise the parallel execution?
Here is the code:
import static org.junit.jupiter.api.Assertions.*;
import java.util.HashMap;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
#Execution(ExecutionMode.CONCURRENT)
public class Parallel {
static String driverPath = "C:/BestX/workspace/chromedriver_win32/chromedriver.exe";
static String downloadFilepath = "C:\\BestX";
#Execution(ExecutionMode.CONCURRENT)
#Test
void test1() throws Exception {
createDriver();
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" => test1"); //.currentThread.getName() -just to display current running thread name
}
#Execution(ExecutionMode.CONCURRENT)
#Test
void test2() throws Exception {
createDriver();
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" => test1"); //.currentThread.getName() -just to display current running thread name
}
public void createDriver() {
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(new String[] { "--no-sandbox" });
chromeOptions.addArguments(new String[] { "--max_old_space_size=4096" });
dcaps.setCapability("goog:chromeOptions", chromeOptions);
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadFilepath);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().window().setSize(new Dimension(1920, 1200));
}
}
'''
and here is the "junit-platform.properties" under resource folder
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
It is workign now. Was a silly mistake in folder structure. The junit-platform.properties file was under tests->resources instead of test->resources folder

How to handle 'Keep' and 'Discard' option while download files in chrome in selenium without AutoIt?

I am using selenium ver. 2.47 and Chrome Ver. 70
I have tried with below code but it didn't worked.
Map<String, Object> prefs = new HashMap<String, Object>();
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
options.addArguments("--safebrowsing-disable-download-protection");
prefs.put("safebrowsing.enabled", "false");
Thanks
Seems you were pretty close. You need to pass the HashMap containing the required configurations to the instance of the ChromeOptions Class as follows:
System.setProperty("webdriver.chrome.driver", "C:/chromedriver/chromedriver.exe");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("safebrowsing.enabled", "true"); //this is the needed configuration
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);

With Selenium 3.8.1, Firefox, Chrome, and IE driver capabilities are deprecated.

Can anybody advise how to remove warnings from FirefoxDriver (capabilities) and
ChromeDriver (capabilities)?
FIREFOX
{
#Override
public DesiredCapabilities getDesiredCapabilities ()
{
DesiredCapabilities capabilities = DesiredCapabilities.firefox ();
return capabilities;
}
#Override
public WebDriver getWebDriverObject (DesiredCapabilities capabilities)
{
return new FirefoxDriver (capabilities);
}
},
CHROME_Original
{
#Override
public DesiredCapabilities getDesiredCapabilities ()
{
DesiredCapabilities capabilities = DesiredCapabilities.chrome ();
capabilities.setCapability ("chrome.switches", Arrays.asList ("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String> ();
chromePreferences.put ("profile.password_manager_enabled", "false");
capabilities.setCapability ("chrome.prefs", chromePreferences);
return capabilities;
}
#Override
public WebDriver getWebDriverObject (DesiredCapabilities capabilities)
{
return new ChromeDriver (capabilities);
}
},
Try to use the following code:
ChromeOptions options = new ChromeOptions();
options.setCapability("chrome.switches",Arrays.asList("--no-default-browser-check"));
HashMap<String, Boolean>chromePreferences = new HashMap<>();
chromePreferences.put("profile.password_manager_enabled", false);
options.setCapability("chrome.prefs", chromePreferences);
ChromeDriver driver = new ChromeDriver(options);
The ChromeDriver constructor now takes in ChromeOptions object as a parameter
Manipulating DesiredCapabilities directly has been deprecated in favor of type-safe “Options” classes (FirefoxOptions InternetExplorerOptions, etc). This has the advantage of helping you avoid setting incorrect or invalid values for the driver. You get rid of the deprecation warnings by changing your code to use the newer, safer construct.

Setup proxy in Firefox browser via marionette driver

Firefox 47 and above do not support Selenium Webdriver. I tried to use a Marionette driver to start my tests via Firefox.
But my settings in firefox-profile (proxy must set to network.proxy.type = 4, auto-detect) is no longer applied to Firefox config (Firefox opens but all settings set by default) and my tests do not work without the right PROXY configuration.
How can I setup proxy in Firefox browser via a Marionette driver?
The old tricks with firefoxProfile or Proxy class do not work any more. All you have to do is pass through requiredCapabilities a JSON with the new Marionette proxy format:
[{proxy={"proxyType":"MANUAL","httpProxy":"host.proxy","httpProxyPort":80,"sslProxy":"host.proxy","sslProxyPort":80}}]
No more "proxyHost:proxyPort" format, but httpProxy=host, httpProxyPort=port.
JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);
required.setCapability("proxy", json);
driver = new FirefoxDriver(service, cap, required);
Here is all the code:
import java.io.File;
import java.io.IOException;
import org.apache.commons.lang.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
public class TestProxy
{
public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("usage: IP port");
System.exit(-1);
}
String proxyServer = args[0];
int proxyPort = Integer.parseInt(args[1]);
try
{
TestProxy test = new TestProxy();
test.TestDriver(proxyServer, proxyPort);
} catch (IOException e)
{
e.printStackTrace();
}
}
private void TestDriver(String aHost, int aPort) throws IOException
{
WebDriver driver = null;
GeckoDriverService service = null;
if (SystemUtils.IS_OS_LINUX)
{
FirefoxBinary firefoxBinary = new FirefoxBinary(new File("/home/ubuntu/firefox/firefox"));
service = new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("/usr/bin/geckodriver"))
.usingAnyFreePort()
.usingAnyFreePort()
.withEnvironment(ImmutableMap.of("DISPLAY",":20"))
.build();
service.start();
}
else if (SystemUtils.IS_OS_WINDOWS)
{
FirefoxBinary firefoxBinary = new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe"));
service = new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("C:\\Windows\\geckodriver.exe"))
.usingAnyFreePort()
.usingAnyFreePort()
.build();
service.start();
}
else
{
System.out.println("Unknown operation system");
System.exit(-1);
}
DesiredCapabilities cap = DesiredCapabilities.firefox();
DesiredCapabilities required = new DesiredCapabilities();
JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);
required.setCapability("proxy", json);
driver = new FirefoxDriver(service, cap, required);
driver.navigate().to("https://api.ipify.org/?format=text");
System.out.println(driver.getPageSource());
driver.quit();
}
}
https://github.com/SeleniumHQ/selenium/issues/2963
https://github.com/mozilla/geckodriver/issues/97#issuecomment-255386464