Selenium Webdriver with Certificate Authentication in Python - python

I am trying to automate the connection to a secure site that requires certificate authentication, in Python. This is from Windows, and does not have to be headless. My organisation does not play well with Firefox, so I have chosen to do this in Chrome. The certificate is a .pfx
I have come across older posts saying it cant be done at all with Chromedriver but a potential workaround would be to create a profile and import the certificate to that profile. When running the code, I reference the profile.
My code so far:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('user-data-
dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\')
driver = webdriver.Chrome(executable_path='C:\\Users\\User\\WinPython-
64bit-3.5.4.0Qt5\\python-
3.5.4.amd64\\chromedriver.exe',chrome_options=options)
time.sleep(3)
driver.get('https://www.securesite.com')
This code results in a "This site can’t be reached" response. When trying it manually, a prompt will popup to choose confirm the certificate to use and I am able to proceed to the site successfully.
When viewing certmgr.mmc I can see the certificate is imported under Personal as well as I have imported it to Trusted Root CA. From Chrome though when I view certificates, I find it only under Personal.
I am not sure what I am doing wrong here, and I am hoping that during recent times - a solution has been found so someone would be able to advise me.
Thank you

Related

Why doesnt Selenium/Chrome Driver open websites with me auto logged in

You guys know how google keeps you signed into different websites? how come whenever I use Selenium/Chrome driver and have it go to a website I'm already signed into, it is like im a fresh user and I have to have Selenium go through the work of signing in, which in some cases I cant because some websites block me from signing in with selenium because they know I'm using a bot.
Google keeps you signed into different websites through your Default profile.
Where as Selenium driven ChromeDriver initiated google-chrome Browsing Context is opened everytime with a newly crated temporary profile.

How do you stay logged in with the Selenium Chrome browser?

I use the following code to open a Chrome window using Selenium:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
However, whenever I enter the site, I am logged out. On other sites, I can manually do so, but Google deems it unsafe to log in with Selenium.
I know that Selenium is a test browser, but is it possible to stay logged in with Selenium so that I can access my user data; or is that not the purpose of Selenium, and calling the Chrome subprocess the way to go?
To start with Selenium isn't a browser as such but a bunch of libraries which helps in simulating Browsing Context.
From the question it's not that clear why you are logged out once you login in to the site. But definitely once you are logged in using Selenium driven ChromeDriver initiated google-chrome Browsing Context you must remain logged in and should be able to access your user data.

When selenium opens Gmail_login, I am not signed in and when I try to sign in, it says the following:

"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."
Is there any way to sign in
This is the code:
from selenium import webdriver
import time
driver =webdriver.Chrome()
driver.get("https://www.gmail.com")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys("********")
time.sleep(5)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/div/button/div[2]").click()
time.sleep(5)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input").send_keys("*******")
time.sleep(5)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/div/button/div[2]").click()
time.sleep(5)
driver.close()
I feel the issue is maybe due to the following reasons -
As you are using a chrome webdriver (basically a automation tool) I think the debugger mode is on and that is why it is not allowing you to sign-in or check if
chrome.exe --remote-debugging-port=9222
is running in your IDE.
Other reason maybe as Google only allows Secure login, you will not be able to login with a unsecure browser. You might have to
Turn On "Less secure app access"
From Google which is not recommended unless or un-till you are using a test Account.
Some apps and devices use less secure sign-in technology, which makes your account vulnerable. You can turn off access for these apps, which we recommend, or turn it on if you want to use them despite the risks. Google will automatically turn this setting OFF if it’s not being used.
If running Chrome normally (out of debug mode) is not an option, then check this https://support.google.com/accounts/thread/22873505?msgid=24501976.

How to handle browser or app may not be secure issue with web driver Selenium python?

I'm trying to automate sign in into gmail and I get to see this error.
I think this must be because the website is able to detect the automation and blocking it. Can you all please tell me how to overcome this?
I don't see this issue with my personal account but this happens only with a common account.
In you account profile, in Security options, try setting this option:
This issue was because of the selenium chrome profile.
I have created a new chrome profile and logged into it with the email id with which I was facing this issue. Then Turn on sync.
With this chrome profile in place I can skip the login steps and directly do the main process.
Use: Chrome Options to add newly created chrome profile as an argument.
Hope this helps people who are facing similar challenge.
I faced this issue.
I have created dummy gmail account and now my selenium script is working fine for the same and able to login successfully.
it's not working for my personal gmail account.
Thanks !!

By pass security code in selenium webdriver python

So basically, I'm new with selenium webdriver (python). I have managed to create plenty of scripts. But recently I having difficulty when the site that I login in using selenium required their user to input the verification code that is sent to their email. Is there any workaround for this? Any suggestions are considered, thanks.

Categories

Resources