Selenium ChromeDriver not going to url using default user data - java

It opens in a new window with the default start menu and shows a notification that Chrome is being controlled by automated test software, but it does not go to the url.
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
What I also tried:
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");
The exception it gives
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.65 seconds
I am using the lastest ChromeDriver 2.30 and Selenium 3.4.0 versions

Found the answer to my own question. It worked when I copied the Default folder from the profile path and moved it somewhere else.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/"+System.getProperty("user.name")+"/Desktop/");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");

Try this:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "CHROME_DRIVER_PATH");
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);
ChromeOptions options = new ChromeOptions();
options.setBinary("CHROME_BINARY_PATH");
options.addArguments("--test-type");
options.addArguments("--allow-running-insecure-content");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = ChromeDriver(capabilities);

If you don't want to move the profile, you can use the below (change %Profile% to the profile you want to use):
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=%Profile%")
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");

Related

Proxy is not working in chromeOptions in webDriver with selenium in Java

I'm using proxy in chrome driver but is it not working it is showing my local network only instead of proxy network.
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setHttpProxy("proxyhost:proxyport");
proxy.setSocksUsername("ProxyUsername");
proxy.setSocksPassword("ProxyPassword";
chromeOptions.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(chromeOptions);
Try below code:
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setHttpProxy("proxyhost:proxyport");
proxy.setSocksUsername("ProxyUsername");
proxy.setSocksPassword("ProxyPassword";
chromeOptions.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(chromeOptions);
Another way of doing same, Please try this
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:password#proxy.com:8080"));
WebDriver driver = new ChromeDriver(capabilities);

selenium headless chrome java ignore ssl errors

Selenium headless chrome testing with java in unix returns empty page source as
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
which was due to accessing the secure http (https) website.
Is there a way to ignore the ssl certificate issue? Please let me know how to ignore it.
Selenium Version 3.7.1..java version 1.8.0.144 chrome driver version 2.33
Chrome Version 62+
I gave a try with options below..but it doesn't seem to work.
1. ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setHeadless(true);DesiredCapabilities capabilities =
DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--
ignore-certificate-errors,--web-security=false,--ssl-
protocol=any,--ignore-ssl-errors=true"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
2. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability (CapabilityType.ACCEPT_INSECURE_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Am i doing this in a right way? Let me know the trick to make it work
Thanks in advance
Complete Code:
WebDriver driver = null;
try {
String filePath = "Path to driver";
System.setProperty("webdriver.chrome.driver", filePath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.setAcceptInsecureCerts(true);
options.addArguments("test-type");
String[] switches = {"--ignore-certificate-errors"};
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList(switches));
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://meta.stackexchange.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("PAGE SOURCE : \n" + driver.getPageSource());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
driver.close();
driver.quit();
}
Now you can add capabilities in option. please try following:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
options.addArguments("--headless", "--window-size=1920,1200", "--ignore-certificate-errors");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
driver = new ChromeDriver(options);

WebDriver to disable SEC_ERROR_UNKNOWN_ISSUER in Firefox

WebDriver opens browser window, but I get SEC_ERROR_UNKNOWN_ISSUER.
I tried to add this site as an exception in browser, but when new browser window opened I get the same message again instead of the website.
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
options.setLogLevel(Level.ALL);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(capabilities);
You need to set setAcceptInsecureCerts Capabilities as true
This simple code work for me :-
System.setProperty("webdriver.gecko.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("https://self-signed.badssl.com/");
OR
System.setProperty("webdriver.gecko.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("acceptInsecureCerts", true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("https://self-signed.badssl.com/")
Change your gecko path in first line of code. Update gecko driver and firefox

How to set proxy for Chrome browser in selenium using Java code

I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.
I tried below code, but still it shows the same error:
Proxy p=new Proxy();
// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");
// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();
// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);
// Open firefox browser
WebDriver driver=new ChromeDriver(cap);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. One way to use a proxy is this:
String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);
Issue is resolved with below code -
Proxy proxy = new Proxy();
proxy.setHttpProxy("yoururl:portno");
proxy.setSslProxy("yoururl:portno");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. You can find new official doc here.
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port");
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");
chromeOptions.setCapability("proxy", proxy);
driver = new ChromeDriver(chromeOptions);
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);
Another way of doing it:
boolean useProxy = true;
ChromeOptions options = new ChromeOptions().addArguments(
'--headless',
'--no-sandbox',
'--disable-extensions',
'--proxy-bypass-list=localhost');
if (useProxy) {
options.addArguments("--proxy-server=http://ProxyHost:8080");
}
WebDriver driver = new ChromeDriver(options);
See https://peter.sh/experiments/chromium-command-line-switches/ for more Chrome switches
Another way to set a proxy:
Proxy proxy = new Proxy();
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability("proxy", proxy);
WebDriver webDriver = new ChromeDriver(chromeOptions);

Java. Selenium. Chromedriver. How can I enable and disable extensions?

I'm using this code to add own extension, but before load the page it automaticly disabels. How can I enable it?
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=...");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
To add an extension and disable the plugins with Chrome:
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// add an extension
options.addExtensions(new File("C:\\extension.crx"));
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[]{
"Adobe Flash Player", "Chrome PDF Viewer"});
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.uk");

Categories

Resources