selenumu unittest
Error 1
TypeError: 'Alert' object is not callable
Reason:
the code wirte error :driver.switch_to.alert()
Do you find the error?
Yes, it should be "driver.switch_to.alert" , no ()
Error 2
driver = self.driver
AttributeError: 'Baidu' object has no attribute 'driver'
Reason:
Error code: def setUP(self):
Find it ?
it should be "setUp", not "setUP"
Othr quesion:
1顽爹、driver.switch_to_alert().accept() #接受Alert彈框
switch_to_alert() 在下個版本將會被棄用齿桃,此處不推薦使用此方法
代替方法:
driver.switch_to.alert.accept()
附上代碼:
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import unittest
import time
class Baidu(unittest.TestCase):
def setUP(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com"
self.verificationErrors = []
self.accept_next_alert = True #是否繼續(xù)接受下一個警告
def test_baidu_serach(self):
driver = self.driver
driver.get(self.base_url+"/")
driver.find_element_by_id("kw").send_keys("Selenium webdriver")
driver.find_element_by_id("su").click()
time.sleep(2)
driver.close()
def test_baidu_set(self):
driver = self.driver
driver.get(self.base_url+"/gaoji/preferences.html") #進入搜索設置頁
Select(driver.find_element_by_id("nr")).select_by_visible_text(u"每頁顯示10條")
time.sleep(2)
driver.find_element_by_id("save").click()
time.sleep(2)
driver.switch_to.alert.accept() # not switch_to.alert()!!!
time.sleep(2)
#保存百度的設置時會會彈出一個確定按鈕烛占;我們并沒按照常規(guī)的方法去定位彈窗上的“確定”按鈕靖苇,而是使用
#driver.switch_to_alert().accept()
def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)
if __name__ == "__main__":
unittest.main()