I am having a problem with geckodriver (FF) in java-selenium.
I am running multiple test scenarios via TEST-NG parallel execution (6+ threads).
Sometimes happens that at the start (opening new browser) I get unreachableBrowserException. So i try-catch it, waited 100ms and try to create it again. it works, but it keeps the first failed browser opened, with a blank page.
Problem is that the first window got opened BEFORE it got initialized as a driver, so it cannot be closed by driver.close() or something (it is null). So I have to close them manually.
So, my question is, has anyone encountered such behaviour?
How can i close such browsers, without influencing the other threads?
while(true) {
try {
setDriver(DriverFactory.createInstance(getBrowserInstance()));
break;
} catch (UnreachableBrowserException e) {
try {
Thread.sleep(100);
log.info("UnreachableBrowserException! Needed to wait for 100ms ");
removeDriver();
counter++;
} catch (Exception e1) {
log.info("Thread could not wait!");
e1.printStackTrace();
}
}
if(counter>100){
log.info("Was not able to create a browser session!");
break;
}
}
and the part of .createInstance :
final ProfilesIni profilesIni = new ProfilesIni();
System.setProperty("webdriver.gecko.driver", "drivers/geckodriver.exe");
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"false");
firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setAssumeUntrustedCertificateIssuer(false);
firefoxProfile.setPreference("app.update.auto", false);
firefoxProfile.setPreference("app.update.enabled", false);
firefoxProfile = profilesIni.getProfile("selenium_profile");
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
return new FirefoxDriver(capabilities); //Here it throws the exception.
Selenium/Java/geckodriver versions are most recent. FF version is unfortunately 44.02 (cannot get newer).
I would be grateful for any tips/hints.
Kind regards,
Martin
As you are using Mozilla Firefox version 44.02 and cannot get newer you have to consider a lot of things.
To use Selenium 3.x you have to mandatory use geckodriver. See why here.
Firefox 44.02 is not marionette enabled, so you have to set marionette to false through DesiredCapabilities class. See why here
There are couple of Selenium version dependencies and GeckoDriver version dependencies. See them here
You cannot create a new Firefox Profile and then again try to use a existing Firefox Profile.
Assuming you have a existing Firefox Profile by the name selenium_profile, the following code would open the Firefox Profile in a new browser session:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile firefoxProfile = profile.getProfile("selenium_profile");
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setAssumeUntrustedCertificateIssuer(false);
firefoxProfile.setPreference("app.update.auto", false);
firefoxProfile.setPreference("app.update.enabled", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
dc.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(dc);
driver.get("http://www.google.com");
Now you can easily return the capabilities.
Related
I'm doing an automated test, using Java (8), Selenium (4) and Chromedriver (98.0).
The test is for a site that requires to login with different third party accounts, one of them being GitLab.Unfortunately the test always gets stuck while trying to access Gitlab's login page, on the "Checking your browser before accessing gitlab.com" part. If I pause the test on this step and duplicate the tab manually, the newly opened tab will be able to enter and then the first one will be also able to do it (probably because at that point finally has a valid cookie).I've tried out different solutions but with no luck. Currently this is my code:
#Test
public void test() throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-blink-features=AutomationControlled");
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
driver.navigate().to("https://gitlab.com/users/sign_in");
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(60))
.pollingEvery(Duration.ofSeconds(2))
.until(x -> driver.findElements(By.xpath("//*[#data-translate='checking_browser']")).size() == 0);
}
When trying to use undetected_chromedriver, using Python, it worked, but it's a requirement for me to use Java. Is there something similar for Java or is there an extra ChromeOption that I'm missing?
need to download chrome driver https://chromedriver.chromium.org/downloadshttps://chromedriver.chromium.org/downloads
and add this code while setting up driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
I am using Selenium in a Burp plugin but I can't load pages with the get method. Browsers open correctly, both Firefox and Chrome, but they don't load the page. Chrome address bar shows "data;.", while Firefox has no text in it. I am using the last driver available, Chrome 81.0.4044.183 and the driver for this exact version, while Firefox is 76.0.1 and I am using GeckoDriver 0.24 (since 0.25+ have a known bug) and it works with the last version of Firefox.
The code is the following
void runBrowserAutomatization(File fileDriver, String seleniumTrack, boolean isHeadless) {
WebDriver driver;
if (gui.usedBrowser().toLowerCase().contains("chrome")) {
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
options.setCapability(CapabilityType.PROXY, proxy);
options.setHeadless(isHeadless);
System.setProperty("webdriver.chrome.driver", fileDriver.getPath());
driver = new ChromeDriver(options);
} else if (gui.usedBrowser().toLowerCase().contains("firefox")) {
FirefoxOptions options = new FirefoxOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
options.setCapability(CapabilityType.PROXY, proxy);
options.setHeadless(isHeadless);
System.setProperty("webdriver.gecko.driver", fileDriver.getPath());
driver = new FirefoxDriver(options);
} else {
PrintMsg("No browser selected...");
return;
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.nytimes.com/");
driver.quit();
}
I may also think it is a Proxy misconfiguration, Burp certificate is installed in Firefox and Windows (where Chrome gets Certificate Authorities) but is not shown in the settings of the instance started by Selenium. Any help or suggestion is highly appreciated, thnaks.
I'm trying to get response and request using BMP's RequestFilter and ResponseFilter. However, when the webpage loads, nothing gets printed in the console.
Everything else seems to work though. Maybe BMP is not watching GeckoDriver?
I'm using Firefox 50.0, BrowserMobProxy 2.1.2, Selenium 3.0.1, and GeckoDriver 0.11.1
The testing code is below. Could someone please help me?
Thank you very much!
BrowserMobProxy server = new BrowserMobProxyServer();
server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
server.start();
int port = server.getPort();
server.addRequestFilter((request, content, info) -> {
String q = URLDecoder.decode(info.getOriginalUrl(), "UTF-8");
System.out.println("Request: "+q);
return null;
});
server.addResponseFilter((response, content, info) -> {
String type = response.headers().get("Content-Type");
System.out.println("Response: "+info.getOriginalRequest());
System.out.println(type);
});
Proxy proxy = ClientUtil.createSeleniumProxy(server);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability("marionette", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
FirefoxProfile fp = new FirefoxProfile();
capabilities.setCapability(FirefoxDriver.PROFILE, fp);
String gecko = "d:/Programming/java/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", gecko);
driver = new FirefoxDriver(capabilities);
driver.get("https://google.com");;
In Firefox 51 and lower, there is a bug/missing feature in Selenium 3's GeckoDriver that prevents Firefox from picking up the proxy settings when setting CapabilityType.PROXY on the DesiredCapabilities object.
However, you can still set the proxy settings directly on the FirefoxProfile. There's an example of this in one of BMP's tests. Since you're already using a FirefoxProfile object, this would probably be a sensible solution for you. It would look something like this (replace localhost with a hostname/ip address as appropriate):
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("network.proxy.http", "localhost");
fp.setPreference("network.proxy.http_port", server.getPort());
fp.setPreference("network.proxy.ssl", "localhost");
fp.setPreference("network.proxy.ssl_port", server.getPort());
fp.setPreference("network.proxy.type", 1);
fp.setPreference("network.proxy.no_proxies_on", "");
This geckodriver issue also discusses a few other alternatives to using CapabilityType.PROXY on the DesiredCapabilities object.
UPDATE
According to the mozilla bug report, this issue is fixed in Firefox 52, which is scheduled to be released on March 7, 2017. In the meantime, the solution with the FirefoxProfile should work with 51 (and lower), and should also continue to work with 52+.
My Firefox version is 46.0.1 and Selenium version is 3.0.1.
I am getting error:
Your connection is not secure
while executing following code:
#Test
public void test() {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("newCretedProfile");
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
System.setProperty("webdriver.gecko.driver", "D:\\SELENUIUM\\Drivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver(ffProfile);
driver.get("http://www.google.com");
driver.quit();
}
I have created new firefox profile and followed steps from this url
Nevertheless it's not working and giving me same error while I launching any site.
Download Firefox 55 beta and set
capabilities.setCapability("acceptInsecureCerts", true);
Here is my code that works for Firefox 55 beta:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setCapability("acceptInsecureCerts", true);
RemoteWebDriver driver = new RemoteWebDriver(Environment.remoteWebDriverURL, capabilities);
I have tried this approach and it worked well for me.
Create new firefox profile by following below step.
Close all your firefox windows
In the Run dialog box, type in: ‘firefox.exe -p' and then Click OK.
Click “Create Profile”
Create a name for your new profile(say Selenium)
Click “Choose Folder”
Pick location something easy to find — like “C:\NewFirefoxProfile”
Click Finish
Now after selecting newly created profile, start Firefox. Open the specific url you were getting 'Secure Connection Issue', accept SSL certificates for this profile.
Now use the newly created firefox profile to run your selenium test. Modify below code as per your requirement.
System.setProperty("webdriver.firefox.marionette","D:\\SELENUIUM\\Drivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("C:\\NewFirefoxProfile");//location of your new firefox profile
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("https://cacert.org/");
With FF v53+ and Se 3.x (summer 2017), advice from before (May?) 2017 is no longer true.
You have to use Marionette and set capability to True.
Took me few days to sort out all old and obsolete advice, yours for free. :-)
Looks like it is not supported yet by geckodriver/Marionette.
You can check below bugs for more information:-
https://github.com/mozilla/geckodriver/issues/93
https://bugzilla.mozilla.org/show_bug.cgi?id=1103196
If you want to run the tests on Firefox with Selenium 3.0 set the Firefox driver capability “marionette” to false.
#Test
public void test() {
DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(d);
driver.get("http://www.google.com");
driver.quit();
}
I am using selenium with PhantomJs to scrape the URL. I initialized the driver as below
final DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"PhantomJsPath");
caps.setCapability("page.settings.loadImages", false);
caps.setCapability("trustAllSSLCertificates", true);
RemoteWebDriver driver = new PhantomJSDriver(caps);
driver.setLogLevel(Level.OFF);
driver.get("https://.......")
The pagesource obtained from the driver is empty
Am I missing anything?
Recently the POODLE vulnerability forced websites to remove SSLv3 support. Since PhantomJS < v1.9.8 uses SSLv3 by default, the page cannot be loaded. To fix this, you would need to run PhantomJS with --ssl-protocol=tlsv1 or --ssl-protocol=any. See this answer for plain PhantomJS.
caps = DesiredCapabilities.phantomjs(); // or new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
new String[] {"--ssl-protocol=tlsv1"});
// other capabilities
driver = new PhantomJSDriver(caps);
If this doesn't solve the issue, you can also add
"--web-security=false", "--ignore-ssl-errors=true"
to the String array of cli args as seen in SiKing's answer here.