-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
59 lines (48 loc) · 2.02 KB
/
hello.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body") != None
def close_last_tab():
if (len(driver.window_handles) == 2):
driver.switch_to.window(window_name=driver.window_handles[0])
driver.close()
driver.switch_to.window(window_name=driver.window_handles[-1])
test_username = raw_input("Enter your username: ")
test_password = raw_input("Enter you password: ")
test_search =raw_input("Enter name of product you want to search: ")
# Load webdriver
#driver = webdriver.PhantomJS()
#driver = webdriver.Chrome()
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
driver = webdriver.Chrome(chrome_options=options)
# Access URL
driver.get("https://www.snapdeal.com/login")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
# Enter email name and check user
email_field = driver.find_element_by_id("userName")
email_field.send_keys(test_username)
driver.find_element_by_id("checkUser").click()
# Enter password and login
wait = WebDriverWait(driver, 10)
password_field = wait.until(EC.element_to_be_clickable((By.ID,'j_password_login_uc')))
password_field.send_keys(test_password)
driver.find_element_by_id("submitLoginUC").click()
# Search product
search_field = wait.until(EC.element_to_be_clickable((By.ID,'inputValEnter')))
search_field.send_keys(test_search)
driver.find_element_by_class_name("searchformButton").click()
# Open first product page
products = wait.until(EC.element_to_be_clickable((By.ID,'products')))
product = products.find_element_by_class_name("product-tuple-listing")
product.click()
close_last_tab()
add_cart = wait.until(EC.element_to_be_clickable((By.ID,'add-cart-button-id')))
add_cart.click()
driver.quit()