I would like to learn how to save and load a session (cookies) with nodriver. I found an example (https://stabler.tech/blog/nodriver-a-new-webscraping-tool), but the code seems to be outdated.
I found some old github incidents, where the developer said something about it and posted some code. (https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1817 // https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1816)
I tried different stuff for hours, even with using Gemini and ChatGPT, but I cant do it. Iam pretty new to Python. Could someone help me?
Here is executable example code:
import asyncio
import nodriver as uc
user = "User"
password = "passwort"
async def type_text(element, text):
for char in text:
await element.send_keys(char)
async def main():
browser = None
try:
browser = await uc.start()
page = await browser.get('https://html-php.de/php/zb_php_f/index.php')
email_field = await page.select("input[name=a_nick]")
password_field = await page.select("input[name=a_pass]")
login_task = None
if email_field and password_field:
await type_text(email_field, user)
await type_text(password_field, password)
login_button = await page.select("input[type=submit]")
if login_button:
login_task = asyncio.create_task(login_button.click())
if login_task:
await login_task
except Exception as e:
print(f"Error during login: {e}")
finally:
if browser:
# Wait for 10 seconds before closing
await asyncio.sleep(10)
await browser.stop()
if __name__ == '__main__':
uc.loop().run_until_complete(main())
I want to achieve, that the second time I start it, its already logged in. (so the exception should get thrown).
Thanks for any help! 🙂
(I couldnt use a “nodriver” tag, so I used “undetected-chromedriver”, its the predecessor)