百度搜索关键词:“简书” 并打开课程页面
from seleniumimport webdriver
from timeimport sleep
import unittest
#百度搜索简书
class Testbaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.driver.get('https://www.baidu.com')
def test_search(self):
driver =self.driver
driver.find_element_by_id("kw").send_keys("简书")
sleep(2)
driver.find_element_by_id("su").click()
self.assertEqual(driver.title,'百度一下,你就知道')
sleep(2)
driver.find_element_by_partial_link_text('创作你的创作').click()
def tearDown(self):
self.driver.quit()
if __name__ =='__main__':
unittest.main
import unittest
test_dir='./'
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_baidu.py')
if __name__ =='__main__':
runner=unittest.TextTestRunner()
runner.run(discover)
运行成功截图
报错
1. unnitest 报错 no attribute'driver'
主要是函数名称写错原来setup 应改为setUp
2. ‘str’ object is not callable
把语句driver=driver.title 放到asserEqual内进行比较