Selenium chromedriver opens a blank page instead of url on linux - java

I am trying to open a url in chromedriver(using RemoteWebDriver) on linux.
I took a screenshot after driver.get(url) is called. It displays a blank page.
east-northamptonshire_screenshot.jpg
I tried this(open a url using ChromeDriver) on my local machine(Windows). It is working fine.
This is the url I am trying to open. "https://publicaccess.east-northamptonshire.gov.uk/online-applications/search.do?action=weeklyList"
Main method:
public static void main(String[] args) throws Exception {
String OS = System.getProperty("os.name").toLowerCase();
WebDriver driver = null;
ChromeDriverService service = null;
boolean isWindows = OS.indexOf("win") >= 0;
logger.info("operating System : " + OS);
if (!isWindows) {
service = new ServerChromeDriver().loadService();
}
driver = new ServerChromeDriver().getPIDriver(service, isWindows);
String url = "https://publicaccess.east-northamptonshire.gov.uk/online-applications/search.do?action=weeklyList";
driver.get(url);
Thread.sleep(3000);
ScreenShot.takeScreenShot(driver);
driver.close();
driver.quit();
service.stop();
}
ServerChromeDriver Class:
public ChromeDriverService loadService() throws Exception {
Configuration configuration = new Configuration();
configuration.loadProperties();
Properties props = new Properties();
try {
props.load(new FileInputStream("config//log4j.properties"));
} catch (IOException e) {
logger.error(e);
}
PropertyConfigurator.configure(props);
service = new ChromeDriverService.Builder().usingDriverExecutable(new File(configuration.getChromeDriverPath()))
.usingAnyFreePort().withEnvironment(ImmutableMap.of("DISPLAY", configuration.getDisplay())).build();
service.start();
return service;
}
public WebDriver getPIDriver(ChromeDriverService service, boolean isWindows) {
WebDriver driver;
if (isWindows) {
driver = new LocalChromeDriver().getDriver();
} else {
driver = new ServerChromeDriver().getDriver(service.getUrl());
}
String hostName = new ServerChromeDriver().getHostName(driver);
logger.info("Running the application on host: " + hostName);
return driver;
}
public WebDriver getDriver(URL serviceUrl) {
Configuration configuration = new Configuration();
configuration.loadProperties();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
// chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--window-size=1800,1800");
// chromeOptions.addExtensions(new File(configuration.getAdBlockPath()));
System.setProperty("webdriver.chrome.driver", configuration.getChromeDriverPath());
System.setProperty("webdriver.chrome.logfile", configuration.getChromeDriverLogFilePath());
System.setProperty("webdriver.chrome.verboseLogging", configuration.getChromeVerboseLogging());
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
capabilities.setJavascriptEnabled(true);
try {
driver = new RemoteWebDriver(serviceUrl, capabilities);
} catch (Exception e) {
logger.error("Error creating a new chrome instance");
throw new RuntimeException(e.getMessage());
}
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
return driver;
}
Application is working fone for this url: https://eplanning.birmingham.gov.uk/Northgate/PlanningExplorer/GeneralSearch.aspx
birmingham_screenshot.jpg
I am using
Headless Chrome : 67.0.3396.62
chromedriver : 2.40.565383
This is what I found from the chromedriver.log file
[0617/144457.403693:ERROR:nss_ocsp.cc(601)] No URLRequestContext for NSS HTTP handler. host: crt.comodoca.com
[0617/144457.403801:ERROR:cert_verify_proc_nss.cc(980)] CERT_PKIXVerifyCert for publicaccess.east-northamptonshire.gov.uk failed err=-8179

I think because Chrome version in your Linux machine is not supported.

For people using newer versions of Selenium who are dealing with this issue, I was able to do the following (similar to Abhilash's answer which is using an older version of Selenium) to resolve the blank-page issue for uncertified domains with Chrome:
ChromeOptions options = new ChromeOptions();
...
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
...
driver = new ChromeDriver(options);
(In my case, webdriver worked fine on my local machine but not on the Linux box hosting CI tools)

After researching about certificate errors, I have added additional capabilities to chrome driver to ignore certificate related errors. Below is my solution.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability("acceptSslCerts", true); // Added this additionally
capabilities.setCapability("acceptInsecureCerts", true); // Added this additionally
capabilities.setCapability("ignore-certificate-errors", true); // Added this additionally

Related

Exception: null when setting up Selenium using ThreadLocal and ChromeDriver (Version 75)?

Current setup stored inside my DriverFactory:
private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
return webDriver.get();
Currently the following method seems to be failing:
public void loadUrl(String url) {
try {
getDriver().get(url);
System.out.println("Successfully navigated to URL: " + url);
} catch (Exception e) {
System.out.println(e.getStackTrace());
Assert.fail("Unable to navigate to URL: " + url + ", Exception: " + e.getMessage());
}
}
Set driver method:
public final void setDriver(String browser) throws Exception {
String remoteHubUrl = "http://xxx.xxxx.xxx.xxx:4444/wd/hub/";
try {
switch (setBrowserType(browser)) {
case "grid":
DesiredCapabilities capabilities =new DesiredCapabilities();
capabilities.setBrowserName("chrome");
ChromeOptions op = new ChromeOptions();
op.merge(capabilities);
webDriver.set(new RemoteWebDriver(new URL(remoteHubUrl), op));
break;
}
}
Exception Message:
Exception: null
There seems to be no issues when using older versions of chromedriver, any ideas?
Base Step which is used to initialise the driver prior to executing the tests:
#Before
public void setupHook() {
setDriver("grid");
}
The main Issue I see with the above code is that you are trying to instantiate a RemoteWebDriver instance using a ChromeOptions object instead of a DesiredCapabilities object.
RemoteWebDriver requires a DesiredCapabilities (See Selenium code here) which ChromeOptions does not extend or implement. They do both extend AbstractCapabilities so you may just have been getting lucky in the past, but now they have diverged to an extent that they are no longer compatible.
*EDIT*
I would suggest you update your code to do this:
switch (setBrowserType(browser)) {
case "grid":
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
webDriver.set(new RemoteWebDriver(new URL(remoteHubUrl),capabilities));
break;
}

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);

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

How can I set the browser version in Selenium RemoteWebDriver?

When I use HtmlUnitDriver I can set my own browserVersion like:
private HtmlUnitDriver initDriver() {
BrowserVersion browserVersion = new BrowserVersion(
BROWSER_NAME,
BROWSER_OS,
USER_AGENT,
Float.parseFloat(BROWSER_VERSION));
browserVersion.setBrowserLanguage(BROWSER_LANGUAGE);
browserVersion.setHtmlAcceptHeader(HTML_ACCEPT_HEADER);
return new HtmlUnitDriver(browserVersion);
}
Is it possible to do the same with the RemoteWebDriver ?
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
myCapabilities);
In Capabilities I can set myCapabilities.setBrowserName("htmlunit"). Is that all that I can do ?
EDIT:
To be clear, I need 3 things:
a) Selenium-server-standalone to be able to reuse the same old SessionID
b) To make browser that works only from console (so no firefox afaik)
c) To make http requests the same as the latest browsers so there will be no difference in the server logs.
You can also set a lot of parameters, for example:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setVersion("35.0");
capabilities.setPlatform(Platform.VISTA);
try {
driver = new RemoteWebDriver(new URL("http://192.168.63.109:5555/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
A bit more settings to skip dialog on file downloading using custom FirefoxProfile:
public static WebDriver setDriver() {
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList", 2);
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
fxProfile.setPreference("browser.download.dir", dir);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel," +
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return new FirefoxDriver(fxProfile);
}

Firebug disabled in WebDriver after adding to profile

I can't seem to figure out why my Firebug extension shows that its disabled when I try to click on it within the Firefox WebDriver.
Here's what my code looks like, I got some of the code from this SO answer:
private final String firefoxExtPath = "/Users/[NAME]/Library/Application Support/Firefox/Profiles/4izeq9he.default/extensions/";
private final String firebugPath = firefoxExtPath + "firebug#software.joehewitt.com.xpi";
private final String firepathPath = firefoxExtPath + "FireXPath#pierre.tholence.com.xpi";
private WebDriver dummy;
private WebDriver driver;
...
#BeforeClass
public void addFirefoxExt() {
// Add extensions to FirefoxDriver
FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(new File(firebugPath));
profile.addExtension(new File(firepathPath));
profile.setPreference("extensions.firebug.currentVersion", "1.11.1");
profile.setPreference("extensions.firebug.onByDefault", true);
profile.setPreference("extensions.firebug.defaultPanelName", "net");
profile.setPreference("extensions.firebug.net.enableSites", true);
} catch (IOException e) {
e.printStackTrace();
}
dummy = new FirefoxDriver(profile);
driver = new FirefoxDriver(profile);
}
#BeforeClass
public void setup() {
dummy.get(BASE_URL);
dummy.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
...
}
That's what i could got from https://code.google.com/p/selenium/wiki/FirefoxDriver. They worked well.
Running with firebug
Download the firebug xpi file from mozilla and start the profile as follows:
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1");
// Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);

Categories

Resources