import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def isElementPresent(self, by, value):
from selenium.common.exceptions import NoSuchElementException
try:
element = self.driver.find_element(by=by, value=value)
except NoSuchElementException as e:
print (e)
return False
else:
return True
def test_isElementPresent(self):
url = "http://www.sogou.com"
self.driver.get(url)
res = self.isElementPresent("id", "query")
time.sleep(2)
if res is True:
print (u"所查找的元素存在于页面上!")
else:
print (u"页面中未找到所需要的页面元素!")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def test_implictWait(self):
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import traceback
url = "http://www.sogou.com"
self.driver.get(url)
self.driver.implicitly_wait(10)
try:
searchBox = self.driver.find_element_by_id("query")
searchBox.send_keys(u"北京")
click = self.driver.find_element_by_id("stb")
click.click()
time.sleep(2)
except (NoSuchElementException, TimeoutException) as e:
traceback.print_exc()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
driver.get("http://www.sogou.com")
input_box=driver.find_element_by_id("query")
wait = WebDriverWait(driver, 10, 0.2)
wait.until(EC.visibility_of(input_box))
input_box.send_keys(u"北京")
wait.until(EC.element_to_be_clickable((By.ID ,"stb")))
button=driver.find_element_by_id("stb")
button.click()
time.sleep(3)
driver.close()
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def test_explicitWait(self):
import traceback
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
url = "http://127.0.0.1/test_explicity_wait.html"
self.driver.get(url)
try:
wait = WebDriverWait(self.driver, 10, 0.2)
wait.until(EC.title_is(u"你喜欢的水果"))
print (u"网页标题是'你喜欢的水果'")
element = WebDriverWait(self.driver, 10).until(lambda x:x.find_element_by_xpath("//input[@value='Display alert box']"))
element.click()
alert = wait.until(EC.alert_is_present())
print("alert=%s" %alert)
print (alert.text)
alert.accept()
peach = self.driver.find_element_by_id("peach")
peachElement = wait.until(EC.element_to_be_selected(peach))
print("peachElement=%s" %peachElement)
checkbox = wait.until(EC.element_to_be_clickable((By.ID, 'check')))
print("checkbox=%s" %checkbox)
print (u"复选框可见并且能被点击")
except TimeoutException as e:
print (traceback.print_exc())
except NoSuchElementException as e:
print (traceback.print_exc())
except Exception as e:
print (traceback.print_exc())
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
-
(4)使用Title属性识别和操作新弹出的浏览器窗口
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="E:\\chromedriver")
def test_identifyPopUpWindowByTitle(self):
from selenium.common.exceptions import NoSuchWindowException, TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import traceback
url = "http://127.0.0.1/test_popup_window.html"
self.driver.get(url)
time.sleep(3)
WebDriverWait(self.driver, 10, 0.2).until(EC.element_to_be_clickable((By.LINK_TEXT, 'sogou 搜索'))).click()
all_handles = self.driver.window_handles
print (self.driver.current_window_handle)
print (len(all_handles))
time.sleep(5)
if len(all_handles) > 0:
try:
for windowHandle in all_handles:
self.driver.switch_to.window(windowHandle)
print (self.driver.title)
if self.driver.title == u"搜狗搜索引擎 - 上网从搜狗开始":
WebDriverWait(self.driver, 10, 0.2).until(lambda x: x.find_element_by_id("query")).send_keys(u"sogou 首页的浏览器窗口被找到")
time.sleep(3)
except NoSuchWindowException as e:
print (traceback.print_exc())
except TimeoutException as e:
print (traceback.print_exc())
self.driver.switch_to.window(all_handles[0])
time.sleep(3)
print (self.driver.title)
self.assertEqual(self.driver.title, u"你喜欢的水果")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
-
(5)操作iframe中的页面元素(iframe使用的比较多,frame现在基本不用)
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def test_HandleIFrame(self):
url = "http://127.0.0.1/frameset.html"
self.driver.get(url)
self.driver.switch_to.frame(0)
assert u"这是左侧 frame 页面上的文字" in self.driver.page_source
self.driver.switch_to.frame(self.driver.find_element_by_xpath("//iframe"))
assert u"这是iframe页面上的文字" in self.driver.page_source
self.driver.switch_to.default_content()
assert u"frameset 页面" == self.driver.title
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def test_Handleconfirm(self):
from selenium.common.exceptions import NoAlertPresentException
import time
url = "http://127.0.0.1/test_confirm.html"
self.driver.get(url)
button = self.driver.find_element_by_id("button")
button.click()
time.sleep(2)
try:
alert = self.driver.switch_to.alert
time.sleep(2)
self.assertAlmostEqual(alert.text, u"这是一个 confirm 弹出框")
alert.dismiss()
except NoAlertPresentException as e:
self.fail("尝试操作的confirm框未被找到")
print (e)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
def test_Cookie(self):
url = "http://www.baidu.com"
self.driver.get(url)
time.sleep(2)
cookies = self.driver.get_cookies()
print("百度首页所有的Cookies如下:")
for cookie in cookies:
print (cookie)
print("*"*100)
ck = self.driver.get_cookie("SUV")
print ("name值为'SUV'的Cookie信息:",ck)
print("*"*100)
print ("name值为“ABTEST”的Cookie信息之后显示:",self.driver.delete_cookie("ABTEST"))
print("*"*100)
self.driver.delete_all_cookies()
cookies = self.driver.get_cookies()
print ("删除全部Cookie后,再次查看Cookies:",cookies)
print("*"*100)
self.driver.add_cookie({"name": "gloryroadTrain", 'value': '1479697159269020'})
cookie = self.driver.get_cookie("gloryroadTrain")
print ("添加自定义Cookie信息:",cookie)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()