1

I'm making autotest with Selenium and Laravel. It's good, but there is a problem with the cookies. I must be logged in every time I run my autotest for website scraping. I want to maintain my login information, so I used the code below in Selenium to store the cookie information for the website.

$driver->manage()->getCookies();

but, there are many cookies in multiple domains, but only cookies from a particular domain are stored. Current connection page

I don't know how to store cookies of all the domains in the cookie list.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Choi yun seok
  • 309
  • 2
  • 15
  • you have to load a page from each domain to get cookies – Corey Goldberg Jul 13 '19 at 14:32
  • can i see example code about my problem.....??? – Choi yun seok Jul 13 '19 at 18:09
  • @CoreyGoldberg not possible if some are redirects.... you can try get example.com and save cookies then go www.example.com and get cookies, but you don't actually get the www.example.com cookies – User Dec 11 '20 at 05:07
  • This might be related to recent changes regarding third-party cookies. One option might be to downgrade the browser you are automating. You could also check out some things that other people tried. https://stackoverflow.com/q/64281247/231316 https://stackoverflow.com/q/64281247/231316 – Chris Haas Dec 11 '20 at 14:51
  • are you using subdomains of a domain like www.example.com and ww1.example.com on your laravel project, or you are using very different domain (in second level) like foo.com and bar.com ? – Abilogos Dec 16 '20 at 17:40

1 Answers1

0

Reusing cookies

If you have stored the cookie from domain example.com, these stored cookies can't be pushed through the webdriver session to any other different domanin e.g. example.edu. The stored cookies can be used only within example.com. Further, to automatically login an user in future, you need to store the cookies only once, and that's when the user have logged in with in the application after successful authentication. Before adding back the cookies you need to browse to the same domain from where the cookies were collected.


Demonstration using Selenium Python

As an example, you can store the cookies once the user have logged in within an application as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
driver.find_element_by_name("username").send_keys("abc123")
driver.find_element_by_name("password").send_keys("123xyz")
driver.find_element_by_name("submit").click()

# storing the cookies
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()

Later at any point of time if you want the user automatically logged-in, you need to browse to the specific domain /url first and then you have to add the cookies as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')

# loading the stored cookies
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    # adding the cookies to the session through webdriver instance
    driver.add_cookie(cookie)
driver.get('http://demo.guru99.com/test/cookie/selenium_cookie.php')

Reference

You can find a detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352