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 #是否继续接受下一个警告
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)
#保存百度的设置时会会弹出一个确定按钮;我们并没按照常规的方法去定位弹窗上的“确定”按钮,而是使用
#driver.switch_to_alert().accept()
def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)
if __name__ == "__main__":
unittest.main()