Creating accounts
Target page: https://slensky.com/zendriver-examples/login-page.html
In this tutorial, we will demonstrate how to fill out a new account sign-up form and then log in with the newly created account. The example page login/signup is implemented entirely with JavaScript, so created accounts do not persist once the tab has been closed.
Feel free to open the page now in your current browser to get an idea of what we will be working with!
Initial setup
Begin by creating a new script for the tutorial:
import asyncio
import zendriver as zd
async def main() -> None:
browser = await zd.start()
page = await browser.get(
"https://slensky.com/zendriver-examples/login-page.html",
)
# TODO: Sign-up and login
await browser.stop()
if __name__ == "__main__":
asyncio.run(main())
Creating a new account
In this example page, you can create a new account by clicking on the "Sign up" link, which makes the sign-up form visible when clicked.
We can create a new function to click this link, fill out the form, and submit it:
async def create_account(page: Tab, name: str, email: str, password: str) -> None:
# Click on the "Sign up" link
sign_up_link = next(a for a in await page.select_all("a") if "Sign up" in a.text)
await sign_up_link.click()
await asyncio.sleep(0.5)
# Fill in the sign-up form
name_input = await page.select("#signupName")
await name_input.send_keys(name)
email_input = await page.select("#signupEmail")
await email_input.send_keys(email)
password_input = await page.select("#signupPassword")
await password_input.send_keys(password)
# Click the "Sign Up" button
sign_up_button = next(
button for button in await page.select_all("button") if "Sign Up" in button.text
)
await sign_up_button.click()
await asyncio.sleep(0.5)
# Click through confirmation dialog
proceed_to_login = await page.find(text="Proceed to Login")
await proceed_to_login.click()
Logging in
Next, filling out the login form and logging in:
async def login(page: Tab, email: str, password: str) -> None:
# Fill in the login form
email_input = await page.select("#loginEmail")
await email_input.send_keys(email)
password_input = await page.select("#loginPassword")
await password_input.send_keys(password)
# Click the "Login" button
login_button = next(
button for button in await page.select_all("button") if "Login" in button.text
)
await login_button.click()
await asyncio.sleep(0.5)
# Verify successful login
message = await page.select("#message")
if "Welcome back" in message.text_all:
print("Login successful")
else:
print("Login failed")
Putting it all together
import asyncio
import zendriver as zd
from zendriver import Tab
async def create_account(page: Tab, name: str, email: str, password: str) -> None:
# Click on the "Sign up" link
sign_up_link = next(a for a in await page.select_all("a") if "Sign up" in a.text)
await sign_up_link.click()
await asyncio.sleep(0.5)
# Fill in the sign-up form
name_input = await page.select("#signupName")
await name_input.send_keys(name)
email_input = await page.select("#signupEmail")
await email_input.send_keys(email)
password_input = await page.select("#signupPassword")
await password_input.send_keys(password)
# Click the "Sign Up" button
sign_up_button = next(
button for button in await page.select_all("button") if "Sign Up" in button.text
)
await sign_up_button.click()
await asyncio.sleep(0.5)
# Click through confirmation dialog
proceed_to_login = await page.find(text="Proceed to Login")
await proceed_to_login.click()
async def login(page: Tab, email: str, password: str) -> None:
# Fill in the login form
email_input = await page.select("#loginEmail")
await email_input.send_keys(email)
password_input = await page.select("#loginPassword")
await password_input.send_keys(password)
# Click the "Login" button
login_button = next(
button for button in await page.select_all("button") if "Login" in button.text
)
await login_button.click()
await asyncio.sleep(0.5)
# Verify successful login
message = await page.select("#message")
if "Welcome back" in message.text_all:
print("Login successful")
else:
print("Login failed")
async def main() -> None:
browser = await zd.start()
page = await browser.get(
"https://slensky.com/zendriver-examples/login-page.html",
)
name = "John Doe"
email = "john.doe@example.com"
password = "securepassword"
await create_account(page, name, email, password)
await login(page, email, password)
await browser.stop()
if __name__ == "__main__":
asyncio.run(main())