import asyncio
from pydoll.browser.chromium import Chrome
from pydoll.extractor import ExtractionModel, Field
class Quote(ExtractionModel):
text: str = Field(selector='.text')
author: str = Field(selector='.author')
tags: list[str] = Field(selector='.tag')
async def main():
async with Chrome() as browser:
tab = await browser.start()
await tab.go_to('https://quotes.toscrape.com/login')
username = await tab.find(id='username')
await username.type_text('john', humanize=True)
password = await tab.find(id='password')
await password.type_text('SecretPass123', humanize=True)
submit = await tab.find(tag_name='input', type='submit')
await submit.click()
logout_link = await tab.find(text='Logout', timeout=5, raise_exc=False)
if not logout_link:
print('Login failed.')
return
quotes = await tab.extract_all(Quote, scope='.quote', timeout=5)
for quote in quotes:
print(f'{quote.author}: {quote.text}')
asyncio.run(main())