#-*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
class FirefoxGerencenterTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors = []
#self.accept_next_alert = True
def test_firefox_gerencenter(self):
driver = self.driver
driver.get(self.base_url + "/")
elem1=driver.find_element_by_id("kw")
elem1.send_keys("python")
elem2=driver.find_element_by_id("su")
elem2.click()
login_id = driver.find_element_by_xpath("//div//a[@id='lb']")
login_id.click()
#driver.find_element_by_id("TANGRAM__PSP_10__pass-pop-login-placeholder-normal").click()
driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("username")
driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys("password")
driver.find_element_by_id("TANGRAM__PSP_10__submit").click()
time.sleep(2)
WebDriverWait(driver,5) #等待页面加载完成
#user_id = driver.find_element_by_xpath("//p//a[@id='s_username_top']")
user_id=driver.find_element_by_link_text("username")
hight_id=ActionChains(driver).move_to_element(user_id)
hight_id.perform()
driver.find_element_by_link_text(u"我的主页").click()
WebDriverWait(driver,5)
main_user_id = driver.find_element_by_xpath("//div//a//span[@class='un']")
main_hight_id=ActionChains(driver).move_to_element(main_user_id)
main_hight_id.perform()
# ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=tiebaPrivacySetting | ]]
driver.find_element_by_link_text(u"退出").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except Exception, 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()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
class FirefoxGerencenterTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors = []
#self.accept_next_alert = True
def test_firefox_gerencenter(self):
driver = self.driver
driver.get(self.base_url + "/")
elem1=driver.find_element_by_id("kw")
elem1.send_keys("python")
elem2=driver.find_element_by_id("su")
elem2.click()
login_id = driver.find_element_by_xpath("//div//a[@id='lb']")
login_id.click()
#driver.find_element_by_id("TANGRAM__PSP_10__pass-pop-login-placeholder-normal").click()
driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("username")
driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys("password")
driver.find_element_by_id("TANGRAM__PSP_10__submit").click()
time.sleep(2)
WebDriverWait(driver,5) #等待页面加载完成
#user_id = driver.find_element_by_xpath("//p//a[@id='s_username_top']")
user_id=driver.find_element_by_link_text("username")
hight_id=ActionChains(driver).move_to_element(user_id)
hight_id.perform()
driver.find_element_by_link_text(u"我的主页").click()
WebDriverWait(driver,5)
main_user_id = driver.find_element_by_xpath("//div//a//span[@class='un']")
main_hight_id=ActionChains(driver).move_to_element(main_user_id)
main_hight_id.perform()
# ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=tiebaPrivacySetting | ]]
driver.find_element_by_link_text(u"退出").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except Exception, 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()