-- coding: utf-8 --
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Taobao(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = “https://www.taobao.com/”
self.verificationErrors = []
self.accept_next_alert = True
def test_taobao(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_link_text(u"登录").click()
driver.switch_to.window(driver.window_handles[1])
页面切换
driver.find_element_by_css_selector("#fm-login-id").send_keys("用户名*******")
driver.find_element_by_id("fm-login-password").click()
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("密码********")
driver.find_element_by_xpath("//button[@type='submit']").click()
driver.switch_to.window(driver.window_handles[1])
driver.find_element_by_id("q").send_keys(u"裤子男")
driver.find_element_by_class_name('btn-search').click()
driver.find_element_by_css_selector('#J_NavCommonRowItems_0 > a:nth-child(3)').click()
driver.find_element_by_id('J_Itemlist_PLink_614856040133').click()
driver.find_element_by_xpath(".//*[@id='J_DetailMeta']/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[2]").click()
driver.find_element_by_xpath(".//*[@id='J_DetailMeta']/div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul/li[1]").click()
driver.find_element_by_id('J_LinkBasket').click()
time.sleep(5)
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if name == “main”:
unittest.main()