'How to headless mode with --user-data-dir=chrome-data chrome option
I am using python selenium for implementing tests. I need to keep login process so I don't log in every time. I use add_argument("--user-data-dir=chrome-data") for saving chrome data, but when I want to use headless mode, the saved data is not keeping login process.
class Browser:
def __init__(self, headless):
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.headless = headless
self.chrome_options.add_argument('--hide-scrollbars')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument("--log-level=3")
self.chrome_options.add_argument("--user-data-dir=chrome-data")
self.chrome_options.add_argument('profile-directory=profile')
self.chrome_options.add_experimental_option("detach", True)
self.driver = webdriver.Chrome(executable_path="./chromedriver", options=self.chrome_options)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
Solution 1:[1]
I had the same issue just recently. As far as I understand, headless and non-headless chrome have two separate userdatas, even if you specify the same folder (bug? feature? hard to tell, even after all the googling). Nothing I was able to do about that.
So anyway, here's what I did in the end. First, add this:
chrome_options.add_argument('--remote-debugging-port=9222')
Then, the code to visit the page and give yourself 5 minutes to log in:
driver.get("http://instagram.com")
time.sleep(300)
When running the code, during the waiting phase, go to your usual Chrome (non-incognito), the chrome://inspect page. It should already be preconfigured — "Discover network targets" should be enabled, and if you press "Configure...", "localhost:9222" should already be in the list (if not, add it and enable the checkbox). And below, you should see your active browser session, press "Inspect" and the window will open, rendering what your headless browser has. Like so: image
So yeah, on your first try, do that, login in your rendered session, and then it should save, and you should be able to continue working afterwards.
Actually, I registered just to thank Kyle from Possible to open/display/render a headless Selenium session? since he's the one who mentioned that since Chrome 100 there's chrome://inspect instead of http://localhost:9222 (the latter showed just a white screen and I was wondering why it didn't work). So I wanted to thank him, but stackoverflow says I don't have enough "reputation" or whatever. Oh well. So I decided to write a reply to one of the recent relevant questions. Let me know if it helped!
Solution 2:[2]
While using headless chrome some websites detect headless mode and prevent autologin
Add this to your chrome options
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36")
This should change your user agent and should help in autologin If the problem still remains you should try specifying default chrome profile directory
self.chrome_options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
You should use this insted
self.chrome_options.add_argument("--headless")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jane Soboleva |
Solution 2 | Noice |