4. Walk through of the Login example - naveens33/selenium_python GitHub Wiki

Lets walk through the login example

Refer login_scenario.py

from selenium import webdriver
driver =webdriver.Chrome(r'..\chromedriver.exe')
driver.maximize_window()
driver.get("http://zero.webappsecurity.com/")
signin=driver.find_element_by_id("signin_button")
signin.click()
driver.find_element_by_id("user_login").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
driver.find_element_by_name("submit").click()
assert "Zero - Account Summary"==driver.title
driver.quit()

Code Analysis:

WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. Detailed explanation of finding elements is available in the Locator Strategy page. find_element_by_* method find the element in the page and return the web element.

signin=driver.find_element_by_id("signin_button")

On the web element the operation can be performed with the help of methods like click(), send_keys() etc.

signin.click()
driver.find_element_by_id("user_login").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
driver.find_element_by_name("submit").click()

Python’s assert statement is a debugging aid that tests a condition. If the condition is true, it does nothing and the program just continues to execute. But if the assert condition evaluates to false, it raises an AssertionError exception with an optional error message.

assert "Zero - Account Summary"==driver.title

Q&A:

Difference between webdriver.get() and webdriver.navigate()

  • navigate().to() navigates to the page by changing the URL like doing forward/backward navigation.

  • get() refreshes the page to changing the URL.