Let's allow selenium to add daily free book for us.
Automation flow:
- Check title of new book
- Login
- Claim new book
- Go to your library
- Check title of last added book
- Compere titles
- Logout
example.py:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import contextlib
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# go to the packtpub page
driver.get("https://www.packtpub.com/packt/offers/free-learning")
driver.implicitly_wait(5)
# store new book title
newBook = driver.find_element_by_xpath('//*[@id="deal-of-the-day"]/div/div/div[2]/div[2]/h2')
nB = newBook.text + ' [eBook]'
try:
login = driver.find_element_by_xpath('//*[@id="account-bar-login-register"]/a[1]/div')
except:
print "you are logged-in"
else:
login.click()
action = webdriver.ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath(
'//*[@id="email"]'
))
action.send_keys("youremail@forloging.com")
action.send_keys(Keys.TAB)
action.send_keys("yourpassword")
action.send_keys(Keys.TAB)
action.send_keys(Keys.RETURN)
action.perform()
getBook = driver.find_element_by_class_name("book-claim-token-inner")
getBook.click();
link0 = driver.find_element_by_xpath('//*[@id="account-bar-logged-in"]/a[1]/div/strong').click()
link1 = driver.find_element_by_xpath('//*[@id="left-side-menu-inner"]/div[3]/a').click()
addedBook = driver.find_element_by_xpath('//*[@id="product-account-list"]/div[1]/div[1]/div[2]/div[1]')
aB = addedBook.text
if aB == nB:
print "hurra! you added newBook " + nB
else:
print "nope"
print aB
print nB
out = driver.find_element_by_xpath('//*[@id="left-side-menu-inner"]/form/input')
out.click()
driver.quit()
Let's try it:
python example.py
No comments:
Post a Comment