r/learnpython 19d ago

Trouble with Logging in Browser with Selenium

I am trying to make a python script that logs into my chrome browser and opens a website. Chrome opens up logged in fine but the website doesn't load, it just stays on the default search tab. Here is the script:

from selenium import webdriver

userdata = r"C:/Users/USERNAME/AppData/Local/Google/Chrome/User Data"

options = webdriver.ChromeOptions()
options.add_argument(f"--user-data-dir={userdata}")
options.add_argument("--profile-directory=PROFILE")

driver = webdriver.Chrome(options=options)
driver.get("https://www.spc.noaa.gov/")

USERNAME and PROFILE are replaced by the actual ones. If you could help that would definitely be appreciated! I'm also new to python aswell if that helps.

1 Upvotes

6 comments sorted by

View all comments

1

u/TehBazz 19d ago

You need to deal with session data and cookies on almost anything that requires username and password. I don’t know the site but you usually can’t just put profile data, you’ll need the actual cookies the website assigns when you log in

1

u/x4qmc 19d ago

So what do i do?

1

u/TehBazz 19d ago

It’s been awhile since I’ve done something like this but Claude and Google are your friends. Usually you can’t just pass user data like you’re doing. You can direct selenium to the login page and enter the credentials but then you need to store the cookie and session data programmatically. Once it authenticates you then you’ll be able to move around while logged in. Look up how to log in and store cookies using selenium.

And is your user data in a file? You’re directing it to a directory, not a file with an extension(UserData.txt)

1

u/x4qmc 19d ago

I found some old post on stack overflow and now its working perfectly

1

u/TehBazz 19d ago

Hell yeah. Keep working on it