今天练习的内容主要写了一个小功能,在百度上搜索某词汇,然后实现web上的back功能
代码如下:
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import time class NavigationTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.driver.get('https://www.baidu.com/') def testBrowserNavigation(self):
driver = self.driver
search_field = driver.find_element_by_name('wd')
search_field.clear() search_field.send_keys('圣女果')
search_field.submit()
time.sleep(1) self.assertEqual('圣女果_百度搜索',driver.title) driver.back()
self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
time.sleep(10)
这里要说的一个方法是:WebDriverWait()until()等待多长时间知道什么出现了,然后才是通过