-1

I am automating a form-filler using selenium, however the issue is the user needs to be logged in to their google account. Selenium is opening up a new browser instance where the user is not logged in. I cannot automate the log in process due to 2 factor authentication.
So far I've found

import webbrowser
webbrowser.open('www.google.com', new = 2)

which will open the window the way I want with the user logged in, however I am unable to interact with the page unlike selenium. Is there a way I can get selenium to open a window like webbrowser? Or is there a way to interact with the page with webbrowser? I have checked the docs of both and not seen an answer to this

imstupidpleasehelp
  • 1,618
  • 2
  • 16
  • 36
  • 1
    Why don't you use selenium with the user profile from your chrome? – Himanshu Poddar Jul 31 '22 at 20:17
  • That's what I am trying to do, but it is opening a new chrome window each time that has no cookies – imstupidpleasehelp Jul 31 '22 at 20:32
  • 1
    So currently you are using the user-data-dir field like this ? `options.add_argument(r"--user-data-dir=C:\Users\hpoddar\AppData\Local\Google\Chrome\User Data")` – Himanshu Poddar Jul 31 '22 at 20:33
  • 1
    Hi there, never developed Selenium code by Python, but it is pretty much same like a Java. Once you opened a web driver by Selenium, google doesn't allow for Selemiun web driver to log in for some security reason. Hence, as long as you open a web browser via Selenium, you cannot log in to Google. – deokyong song Aug 01 '22 at 01:43

1 Answers1

-1

You don't need to make the user log in again to their user account. You can use the same chrome profile that you have for your browser. This will enabled you to use all your accounts from chrome without making them log explicitly.

Here is how you can do this :

  • First get the user chrome profile path Mine was : C:\Users\hpoddar\AppData\Local\Google\Chrome\User Data

  • If you have multiple profiles you might need to get the Profile id for your chrome google account as well.

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver  
  
chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"

options = webdriver.ChromeOptions()
  
options.add_argument(r"--user-data-dir=C:\Users\hpoddar\AppData\Local\Google\Chrome\User Data")
# Specify this if multiple profiles in chrome
# options.add_argument('--profile-directory=Profile 1')

s = Service(chrome_path)
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.co.in")
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
  • This isn't really the solution I am looking for, this would require every user to track down their chrome profile path and it would not work for other web sites. I am mainly looking for a way to simple use the browser that I mainly use just like `webbrowser` does – imstupidpleasehelp Jul 31 '22 at 20:42