
Hello, Welcome to Selenium tutorial in this post we will see how to Handle Untrusted Certificate Selenium.
- What is Untrusted SSL certificate? Whenever We try to access HTTPS website or application so many time you will face untrusted SSL certificate issue. This issue comes in all browser like IE, Chrome,Safari, Firefox etc.
2- Why we get this certificate issues often?
This certificates some in multiple conditions and we should know all of them so that we can rectify them easily.
1- Each secure site has Certificate so its certificate is not valid up-to-date.
2– Certificate has been expired on date
3– Certificate is only valid for (site name)
4- The certificate is not trusted because the issuer certificate is unknown due to many reasons.
Handle Untrusted Certificate Selenium
Step 1-We have to create FirefoxProfile in Selenium.
Step 2- We have some predefined method in Selenium called setAcceptUntrustedCertificates() which accept Boolean values(true/false)- so we will make it true.
Step 3-Open Firefox browser with the above-created profile.
I have published video on the same.
Handle untrusted certificate in Firefox
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; public class SSLCertificate { public static void main(String[] args) { //It create firefox profile FirefoxProfile profile=new FirefoxProfile(); // This will set the true value profile.setAcceptUntrustedCertificates(true); // This will open firefox browser using above created profile WebDriver driver=new FirefoxDriver(profile); driver.get("pass the url as per your requirement"); } }
Since Firefox comes default browser in Selenium so for other browsers like Chrome, IE, Safari we have to use below technique.
Handle untrusted certificate in Chrome
// Create object of DesiredCapabilities class DesiredCapabilities cap=DesiredCapabilities.chrome(); // Set ACCEPT_SSL_CERTS variable to true cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Set the driver path System.setProperty("webdriver.chrome.driver","Chrome driver path"); // Open browser with capability WebDriver driver=new ChromeDriver(cap);
Handle untrusted certificate in IE
// Create object of DesiredCapabilities class DesiredCapabilities cap=DesiredCapabilities.internetExplorer(); // Set ACCEPT_SSL_CERTS variable to true cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Set the driver path System.setProperty("webdriver.ie.driver","IE driver path"); // Open browser with capability WebDriver driver=newInternetExplorerDriver(cap);
Handle untrusted certificate in Safari
// Create object of DesiredCapabilities class DesiredCapabilities cap=DesiredCapabilities.safari(); // Set ACCEPT_SSL_CERTS variable to true cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Set the driver path System.setProperty("webdriver.safari.driver","Safari driver path"); // Open browser with capability WebDriver driver=new SafariDriver(cap);
I would suggest you add above code in Base Class in Selenium Webdriver so you don’t have to write code again and again.
I have implemented in my office project and found good results.
Thanks for visiting my blog. Please comment below if you finding any issue.
Keep in touch. Have a nice day
Hi Mukesh,
I hope u doing great. I m automating my project. One of my test cases is to sign-up using Gmail But when executing sign-up using Gmail, after typing username and click the next button, Chrome show error
“This browser or app may not be secure. Learn more
Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.”
Can you help me with this? Looking forward to your reply
Thanks
Regards
Omair
Hi Omair,
Google has restricted the usage of automation of Google account sign up and sign in action. Google considers this as bots. You can use this link https://opensource-demo.orangehrmlive.com/ for your learning purpose
Hi Mukesh,
Why we are creating a profile for firefox? Why can’t we use DesiredCapabilites or firefox options for firefox?
Hi Gaurav,
The above blog post is for Selenium 2.xx. Now Selenium 3.6 version onwards gecko driver has introduced FirefoxOptions similar to ChromeOptions. If you are using latest Selenium version then use FirefoxOptions
Hi Mukesh,
I tried your code for chrome browser and it is not working.
My Chrome Version: 69
Selenium Version : 2.52.0
Getting SSL pop up everytime after entering the URL although i have added that certificate in my chrome browser as well.
Thanks
Vishnu Verma
Hi Vishnu,
Try this code…
ChromeOptions option = new ChromeOptions();
option.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(option);
Hi Mukesh,
I tried both DesiredCapabilities and FirefoxProfile option, but still facing issue in launching https URLs like google,facebook. FF 50 and SE 3.0.1, also i tried with 2.53.1 but getting same issue
Hi Sanjay,
I just tried and I am able to handle certificate.
Hi Mukesh ,
I tried both DesiredCapabilities and
driver.get(“ur app URL”);
driver.navigate().to(“javascript:document.getElementById(‘overridelink’).click()”);
to handle SSL for IE 11 browser . But its worn’t work for me .
Can you please tell how to handle SSL for IE 11browser .
Below is the SSL message . which we need to handle .
“We recommend that you close this webpage and do not continue to this website. “.
Thank You.
Hi there,
The code statements you have written over are correct. Moreover you need to check Internet Options security. For this please visit this link http://learn-automation.com/challenges-with-ie-browser-in-selenium-webdriver/
Hi Mukesh,
I tried both DesiredCapabilities and FirefoxProfile option, but still facing issue in launching https URLs like google,facebook. FF 50 and SE 3.0.1
Hi Bhavik,
Yes it is issue with Selenium 3 so you can try with 2.53.1
profile.setAcceptUntrustedCertificates(true);
WebDriver driver=new FirefoxDriver(***profile***);
Mukesh, you also seem to have forgotten to pass the profile object in your first piece of code. With SSL certs it is difficult to test whether or not the code works even without passing this object. My contention is it won’t.
Example:
profile.setPreference(“browser.startup.homepage”, “http://google.com”);
If we try the above line of code (that sets the profile homepage) with the below 2 statements separately, it works only when the profile object is passed as a parameter.
WebDriver driver = new FirefoxDriver(profile);
AND
WebDriver driver = new FirefoxDriver();
Not sure if this one too was unintentional. Please let me know what you think.
Thanks Venu and post updated.
WebDriver driver=new SafariDriver(***cap***);
You forgot passing the “cap” instance in your last line of code.
A minor typo but worth correcting.
Hey Venu,
Thank you. Post updated 🙂
Hi Mukesh,
I am using your tutorial to learn Selenium. Now I have come so far in Selenium. Thanks for these tutorials and videos. You are a good teacher and its helping me alot.
I have one doubt regarding use of thread. I find your codes are having thread command . Is it good to use thread (sleep) in code.? In real application are we using it? or can we use it?
sleep method is not good practise to use.
Kindly refer http://learn-automation.com/explicit-wait-in-selenium-webdriver/
Hi
This is my code
public static void main(String[] args) {
DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty(“webdriver.ie.driver”, “C:\\Selenum\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe”);
WebDriver driver = new InternetExplorerDriver(cap);
driver.get(“https://10.216.2.59/BritishLand/Live/Themes/Tabs/Login.aspx?ReturnUrl=%2fBritishLand%2flive”);
but still getting the same page
HI Swapna,
Try this as well for IE browser
driver.get(“https://xyz.com”);
//now we are going to use javascipt ,This will click on “Continue to this website (not recommended)” text and will //push us to the page
driver.navigate().to(“javascript:document.getElementById(‘overridelink’).click()”);
Very Nice Explanation and information. Cheers!!!
Thanks Jithendra 🙂
Hi Mukesh,
yesterday i have gone through your 03 videos [Generate Logs using Logger, Security Certificate,Windows Popup handling].
i was strangling from 01 week to clear this things but yesterday i have followed you videos and created sample application and within 40 Minutes i learnt all these.
I have few questions [small questions]:
1. which is better CSS or XPATH and why ?
2. How you will control page if Network Goes down while entering data in selenium webdriver
Thanks,
Vishnu GOdse
Hey Vishnu,
Really happy to see nice comment 🙂 Hope my future videos will also help you.
Please find my answers
1. which is better CSS or XPATH and why ?
Ans- CSS is faster and it works same in all browser. Xpath works fine with FF and Chrome.
2. How you will control page if Network Goes down while entering data in selenium webdriver
Ans- If network goes down it will throw no such element exception. To handle this you can use smart wait.
What is the difference in profileini and firefoxfile() method, profileini also use for SSL Error??
will this code also work for “Error code: sec_error_unknown_issuer”
Yes it should work.
Mine did not :'(
Hi Richmond,
In this browser you tried?
Im using Firefox47, using marionette and im using RemoteWebDriver
Hey Richmond,
I have not used marionette so not sure on this 🙁 why dont you use FF 46 ?
very nicely explained…keep it up with a good work @Mukesh
Thanks Vidya, Let me know if any help from my side.
I tried with IE11 and it didn’t work for the site driver.get(“https://cacert.org/”); could you please check.
Let me try and will update u soon.
Thanks you!
Thanks Raja
Hi there,
I am trying to use selenium webdriver for Safari browser. As you mentioned in your post regarding Safari launching, I’ve one doubt. Do we need to provide safari driver path because it is available as extension to Safari browser which we can install it as ‘Open With’ option in windows-> Safari.
And if you go to Safari Settings-> Preferences-> Extensions tab. Here I’ve already enabled it
Could you pls put some light over this.
Regards,
Lajish Lakshmanan
Hi Lajish,
Currently safari extension is available for 2.45 only so you can use Selenium version 2.45 or lower it will work fine. Right now i tried with 2.40 its working fine in my machine.
Hi there,
Do i need to provide safari driver path ?
As same thing is already available as extension to browser.
No Lajish, just install the extension and it will work. Previously we had driver for safari browser but now extension will perform the same thing.