Selenium Webdriver - Cookies with Python WebDriver

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 พ.ย. 2024

ความคิดเห็น • 80

  • @3Dbotronix
    @3Dbotronix 3 ปีที่แล้ว +1

    thank you very much
    I was looking for this video from last two day but no one give that practical idea.

    • @ArturSpirin
      @ArturSpirin  3 ปีที่แล้ว

      Glad it was helpful!

  • @usingvancedplzdontban1128
    @usingvancedplzdontban1128 4 ปีที่แล้ว +1

    Thanks man EXCELLENT tut, subscribed! Just a suggestion: please make text size of your code editor slightly bigger so it is easier to view on mobile as well, thanks. Please continue creating advanced Selenium python tutorials. :)

  • @xavierpierre5586
    @xavierpierre5586 3 ปีที่แล้ว +1

    your code save me a lot of time for handling cookie really big thank to you

  • @chasingwildlife6584
    @chasingwildlife6584 3 ปีที่แล้ว +1

    Great stuff. Thanks for the tutorial. A neat and tidy explanation and at a nice pace to follow along while I try to implement it. Your work is appreciated :)

    • @ArturSpirin
      @ArturSpirin  3 ปีที่แล้ว

      I'm glad it was helpful! Thank you for the comment!

  • @shivamkumraa
    @shivamkumraa 5 ปีที่แล้ว +6

    awesome bro you saved my life

  • @SweeperAndRackshasa
    @SweeperAndRackshasa 2 ปีที่แล้ว

    Thank you so much! my script didn't work exactly as yours but i could manage to make it work XD

  • @opportunity2crisis
    @opportunity2crisis 2 ปีที่แล้ว

    Really helped me a lot . Thanks

  • @duybui4112
    @duybui4112 3 ปีที่แล้ว +1

    please help me!
    when i used the code from github or when i typed the exact code from youtube
    i get the error text "selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain"

    • @deluxe4625
      @deluxe4625 3 ปีที่แล้ว

      same did u fix it?

  • @atifmohammed1903
    @atifmohammed1903 4 ปีที่แล้ว +4

    Thank you sir!
    This was an awesome tutorial!

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว +1

      Glad you enjoyed it!

    • @harisfaraz5407
      @harisfaraz5407 4 ปีที่แล้ว

      Atif Mohammed ... hello please tell me how can use it . I need it

  • @miletichakarov5983
    @miletichakarov5983 2 ปีที่แล้ว +1

    Great content, well done tutorial.Worth subs.Cheers!

  • @Vicmendon
    @Vicmendon 4 ปีที่แล้ว +1

    Very very very cool. Tks! I've searching about this, but nothing works. Now it's running!

    • @harisfaraz5407
      @harisfaraz5407 4 ปีที่แล้ว

      how can use it plese tell me

  • @yasofficiel5838
    @yasofficiel5838 3 ปีที่แล้ว

    Followed your video, but I received this error: ConnectionRefusedError: [Errno 61] Connection refused. The error came, when i tried to save_cookie. How can i fix this error? The cookie wall i am trying to bypass is just two buttons , accept or dont accept , and therefore no login.

  • @codewithkolhar3131
    @codewithkolhar3131 4 ปีที่แล้ว +1

    Thanks a lot sir. You made my day. You are great.....

  • @sa6kad
    @sa6kad 4 ปีที่แล้ว +2

    Больше спасибо за видос!

  • @doecommerce1439
    @doecommerce1439 3 ปีที่แล้ว

    Dear, Thank you for your content. I'm just wondering if you can make a quick video to explain how can we connect to an existing chrome using selenium ?
    I mean use a specific profil in chrome with selenium instead of open a new chrome without session when you launch it.
    I saw many different thing on slack but most of code is java and isn't works.
    I really appreciate if you can do a video about this.
    Thank you.

  • @virinchis246
    @virinchis246 3 ปีที่แล้ว

    Second most popular use case :P

  • @fiziwee386
    @fiziwee386 3 ปีที่แล้ว

    you mention that you have some code that checks the health of the driver and if it detects the driver is in a bad state it kills it and restarts the driver. What is that script or where can i learn more about that?

    • @ArturSpirin
      @ArturSpirin  3 ปีที่แล้ว

      That code is not available and I can't share it as its being used in a proprietary software. But you can create your own logic based on the conditions you define that will flag the driver to be in "bad state" and recovering it as easy as killing it completely and then creating a new instance and if you require, you can load the cookies as well.

  • @stroestroe3098
    @stroestroe3098 4 ปีที่แล้ว +1

    This code works great for facebook, yahoo email, etc. But I work with this site, that requires an Email Security Verification code (meaning I enter the email and password for the account, but they send me a verification code via email, that needs to be inserted) and this cookies method doesn't seem to work. Can you suggest me a way to manipulate cookies in my case? Regardless, great video.

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว

      The idea of cookies is to save your current session and be able to restore it later. Sessions, however, do expire. Some expire in a day some expire in 5 min of inactivity (banking sites) and you can't restore an expired session with cookies, it will ask you to auth again. So you need to figure out how to create a new session in case the one you are using no longer valid.

  • @viggogroth3617
    @viggogroth3617 3 ปีที่แล้ว

    Is it possible to login to automate this with the cookie value only and without the need to login in the first place?

    • @ArturSpirin
      @ArturSpirin  3 ปีที่แล้ว

      No. Website needs to create a session to authenticate you without you having to login again. To create a session, you first need to login. Cookie carries your session information which allows the website backend to tell if your session and valid/active.

  • @idensas
    @idensas 3 ปีที่แล้ว

    Hello, can someone help me please, I dont know how to solve this problem and where is the solution, When I loading cookies by FOR on the first circle he is stopping and saying "nvalidCookieDomainException: Message: invalid cookie domain: Cookie 'domain' mismatch".

    • @deluxe4625
      @deluxe4625 3 ปีที่แล้ว

      same here did u fix it?

  • @alicia-from-brazil
    @alicia-from-brazil 4 ปีที่แล้ว +1

    thank you! helped a lot

  • @ion5683
    @ion5683 3 ปีที่แล้ว

    Hey, great video! I have one problem.
    When I load the page after I've already saved the cookies, it is still asking for credentials. What am I doing wrong? Please help!

    • @ArturSpirin
      @ArturSpirin  3 ปีที่แล้ว +1

      First step is to check if indeed the cookies are being loaded by printing the content of the cookies and seeing what domains are there and if your domain is there. If the cookies are loading, check with your site admin to see what they are using to tell if a user has active or expired session and how/when they are redirecting to the login screen.

  • @erickalvarez6486
    @erickalvarez6486 4 ปีที่แล้ว +1

    Thanks a lot man, this works great as hell though I didn't understand why whould I need the "delete_cookies" method, I didn't hear the whole video since I'm the kind of person that can't handle too much noise coming from a mouth.

  • @lukeramsingh8961
    @lukeramsingh8961 4 ปีที่แล้ว +1

    Great tutorial!
    However, I encountered an issue. I've followed the procedure exactly but am getting this message in cookies.txt :
    "File was loaded in the wrong encoding: windows-1254"
    I have tried changing the file encoding (in Pycharm) however it either continues to display that message or the program isn't able to bypass the cookie popup.

  • @officialgamers2986
    @officialgamers2986 4 ปีที่แล้ว +1

    how can i get cookies

  • @ArtemSvirid
    @ArtemSvirid 4 ปีที่แล้ว +1

    big thanks

  • @joexu9258
    @joexu9258 4 ปีที่แล้ว +1

    excellent, thx

  • @sir6681
    @sir6681 4 ปีที่แล้ว +1

    How can u load cookies from www.hackerrank.com when u are on google.com
    I have an error.
    Cookies may only be set for the current domains

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว

      Go to hackerrank and then load cookies for hackerrank, after that you can navigate where you need to, your cookies will remain.

  • @harisfaraz5407
    @harisfaraz5407 4 ปีที่แล้ว

    hello , Artur Spirin
    ////////////////////// how to use this code . I need this help me

  • @xuan1991
    @xuan1991 3 ปีที่แล้ว

    how to login google by cookie?

  • @notAvn
    @notAvn 3 ปีที่แล้ว +1

    i like the heavy breathing

  • @abhimanyujoshi1816
    @abhimanyujoshi1816 4 ปีที่แล้ว

    Is there a way to store third party cookies as well?

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว +1

      You should be able to iterate over the cookie object and see which domains you have cookies stored for, then use that domain to load the cookie. If there are any third party cookies, they all will be part of the cookie object.

    • @abhimanyujoshi1816
      @abhimanyujoshi1816 4 ปีที่แล้ว

      Artur Spirin thanks, will try that.

  • @EnglishRain
    @EnglishRain 4 ปีที่แล้ว

    Hey, excellent tutorial! It works on Twitter.com. However, when i tried to do it on web.whatsapp.com, it didn't work. Basically it is not able to save the cookies in the cookies.txt file when i run your python function. Any idea on why it doesn't save? & any idea on solution for it? Thanks.

    • @miguellopes5108
      @miguellopes5108 4 ปีที่แล้ว +1

      Hey, did you manage to solve that error? im getting the same problem

    • @EnglishRain
      @EnglishRain 4 ปีที่แล้ว +1

      @@miguellopes5108 Yeah i realized the solution was to use a saved browser session instead of saving cookies. So try that instead.

    • @juanwea223
      @juanwea223 3 ปีที่แล้ว

      @@EnglishRain How do you use a saved browser session in selenium?

  • @gilsonmiranda8372
    @gilsonmiranda8372 5 ปีที่แล้ว +1

    Great!! Thanks

  • @Dideulle97
    @Dideulle97 5 ปีที่แล้ว +1

    nice :)

  • @ArksYawron
    @ArksYawron 4 ปีที่แล้ว

    PLEASE HELP! All I did was input my desired page and it gave me the error below. Below is my code. the ***** where the credentials would be for the login info was intentional, and not present within my actual code.
    import pickle
    import pprint
    import time
    from selenium import webdriver
    def save_cookies(driver, location):
    pickle.dump(driver.get_cookies(), open(location, "wb"))
    def load_cookies(driver, location, url=None):
    cookies = pickle.load(open(location, "rb"))
    driver.delete_all_cookies()
    # have to be on a page before you can add any cookies, any page - does not matter which
    driver.get("google.com" if url is None else url)
    for cookie in cookies:
    driver.add_cookie(cookie)
    def delete_cookies(driver, domains=None):
    if domains is not None:
    cookies = driver.get_cookies()
    original_len = len(cookies)
    for cookie in cookies:
    if str(cookie["domain"]) in domains:
    cookies.remove(cookie)
    if len(cookies) < original_len: # if cookies changed, we will update them
    # deleting everything and adding the modified cookie object
    driver.delete_all_cookies()
    for cookie in cookies:
    driver.add_cookie(cookie)
    else:
    driver.delete_all_cookies()
    # Path where you want to save/load cookies to/from aka C:\my\fav\directory\cookies.txt
    cookies_location = "C:\Castle Roofing & Construction\DO NOT TOUCH/cookies.txt"
    # Initial load of the domain that we want to save cookies for
    # chrome = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome/chromedriver.exe")
    # chrome.get("app.jobnimbus.com/default.aspx")
    # chrome.find_element_by_xpath("/html/body/div[1]/form/div[3]/div/div[3]/div[2]/div[1]"
    # "/div[2]/div[2]/div[2]/input").send_keys("*******")
    # chrome.find_element_by_xpath("/html/body/div[1]/form/div[3]/div/div[3]/"
    # "div[2]/div[1]/div[2]/div[3]/div[2]/input").send_keys("********")
    # chrome.find_element_by_xpath("/html/body/div[1]/form/div[3]/div/div[3]/"
    # "div[2]/div[1]/div[2]/div[6]/div[1]/input").click()
    # save_cookies(chrome, cookies_location)
    # chrome.quit()
    # Load of the page you cant access without cookies, this one will fail
    # chrome = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome/chromedriver.exe")
    # chrome.get("app.jobnimbus.com/default.aspx")
    # Load of the page you cant access without cookies, this one will go through
    chrome = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome/chromedriver.exe")
    load_cookies(chrome, cookies_location)
    chrome.get("app.jobnimbus.com/default.aspx")
    # chrome = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome/chromedriver.exe")
    # chrome.get("google.com")
    # time.sleep(2)
    # pprint.pprint(chrome.get_cookies())
    # print "=========================
    "
    #
    # delete_cookies(chrome, domains=["www.google.com"])
    # pprint.pprint(chrome.get_cookies())
    # print "=========================
    "
    #
    # delete_cookies(chrome)
    # pprint.pprint(chrome.get_cookies())
    Traceback (most recent call last):
    File "C:/Users/lucas_000/PycharmProjects/Experimenting with Selenium/testingcookies.py", line 63, in
    load_cookies(chrome, cookies_location)
    File "C:/Users/lucas_000/PycharmProjects/Experimenting with Selenium/testingcookies.py", line 20, in load_cookies
    driver.add_cookie(cookie)
    File "C:\Users\lucas_000\PycharmProjects\Experimenting with Selenium\venv\lib\site-packages\selenium\webdriver
    emote\webdriver.py", line 894, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
    File "C:\Users\lucas_000\PycharmProjects\Experimenting with Selenium\venv\lib\site-packages\selenium\webdriver
    emote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
    File "C:\Users\lucas_000\PycharmProjects\Experimenting with Selenium\venv\lib\site-packages\selenium\webdriver
    emote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry'
    (Session info: chrome=80.0.3987.132)

  • @ankitttyagi20081
    @ankitttyagi20081 5 ปีที่แล้ว

    Hello artur . Getting error
    traceback (most recent call last);
    Line 41 , in
    load_cookies(fire ,"E:\cookies\cookies.txt")

    • @ankitttyagi20081
      @ankitttyagi20081 5 ปีที่แล้ว

      Location of the cookies text file is the same as mentioned in the comment

  • @thiagomilitino7867
    @thiagomilitino7867 5 ปีที่แล้ว

    hi, i tried this code in another url, but recive this except (Message: Cookies may only be set for the current domain (www.google.com)) Can you help me?

    • @ArturSpirin
      @ArturSpirin  5 ปีที่แล้ว

      Hello, if you can share your full code snippet in like a Gist, I might be able to help.

    • @Redeemer54
      @Redeemer54 5 ปีที่แล้ว +1

      same problem code is the same, but url is changed

  • @officialgamers2986
    @officialgamers2986 4 ปีที่แล้ว +1

    Can you do this too gmail

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว

      Yes, should be able to do it to gmail. Anything that relies on cookies to retrieve session will work with this method.

  • @officialgamers2986
    @officialgamers2986 4 ปีที่แล้ว

    plz reply

  • @QA-cx9jz
    @QA-cx9jz 4 ปีที่แล้ว

    Русской версии видео нет?

    • @ArturSpirin
      @ArturSpirin  4 ปีที่แล้ว

      No Russian version, unfortunately.

  • @ankitttyagi20081
    @ankitttyagi20081 5 ปีที่แล้ว

    I am using fire= webdriver.firefox()

  • @ankitttyagi20081
    @ankitttyagi20081 5 ปีที่แล้ว

    Please help

  • @theodoregamerkid1565
    @theodoregamerkid1565 4 ปีที่แล้ว

    wh at

  • @silovoymisha
    @silovoymisha 5 ปีที่แล้ว

    you from Russia?

    • @ArturSpirin
      @ArturSpirin  5 ปีที่แล้ว +1

      yes

    • @silovoymisha
      @silovoymisha 5 ปีที่แล้ว +1

      @@ArturSpirin на русском общаешься? )))

    • @ArturSpirin
      @ArturSpirin  5 ปีที่แล้ว

      @@silovoymisha Yes, you can contact me on LinkedIn