-1

I'm running a simple scrape code to scrape a few lines from a website. The problem is that Python always opens a new Chrome window and logs in again every time I run the bot.

So, I read online and created a Chrome profile. Now it opens the Chrome profile, but it still asks me to log in to the website. I guess I need to save cookies. I tried some YouTube tutorials, but I couldn't figure out how to save the cookies. I'm a complete noob, so can anyone explain me how to do so?

This is my code:

options = Options() 
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', chrome_options=options)
driver.get("https://websitetologin.com")

search = driver.find_element_by_name("fm-login-id")
search.send_keys("loginemail")
search.send_keys(Keys.RETURN)

time.sleep(3)
search = driver.find_element_by_name("fm-login-password")
search.send_keys("loginpassword")
search.send_keys(Keys.RETURN)

time.sleep(3)

search = driver.find_element_by_class_name("fm-button")
search.send_keys(Keys.RETURN)
time.sleep(3)
Nat Riddle
  • 928
  • 1
  • 10
  • 24

1 Answers1

0

You can use the chrome options as well user-data-dir=selenium

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(options =options)

it would save cookies for current session, that can be used later for profiles and folders.

You can refer here for more

or

driver.get('http://google.com')
for cookie in cookies:
    driver.add_cookie(cookie)
cruisepandey
  • 28,520
  • 6
  • 20
  • 38