class TestFindTable(unittest.TestCase): def setUp(self): self.browser = Browser().browser(location="../../lib/chromedriver.exe") self.common = Common(self.browser) self.common.get() def tearDown(self): self.browser.quit() def test_find_element_by_css_selector(self): # 获取整个table对象 table = self.browser.find_element_by_id("table") # 获取所有行 tr_list = table.find_elements_by_tag_name("tr") # 先断言获取的行数是否正确 assert len(tr_list) == 5, "表格行数错误!" for row in tr_list: # td_list = row.find_elements_by_tag_name("td") # for col in td_list: # print(col.text, end="\t") # print() # 获取表格第二行第二列单元格,获取表格内容必须先通过 tbody self.browser.find_element_by_xpath( "//*[@id='table']/tbody/tr[2]/td[2]") self.browser.find_element_by_css_selector( "table#table > tbody > tr:nth-child(2) > td:nth-child(2)") # 获取表格的子元素 input 的value值 cream = self.browser.find_element_by_xpath( "//td[contains(., '化妆')]/input[1]").get_attribute("value") self.assertEqual(cream, "面霜", "找不到面霜文本!")
class TestById(unittest.TestCase): def setUp(self): self.browser = Browser().browser(location="../../lib/chromedriver.exe") self.report_dir = ReportDirectory() self.common = Common(self.browser) self.common.get() def tearDown(self): self.browser.quit() def test_find_element_by_id(self): try: self.browser.find_element_by_id("username") self.browser.find_element_by_id("passwords") except Exception: self.report_dir.get_screenshot(self.browser) raise
class MyClass(unittest.TestCase): def setUp(self): self.browser = Browser().browser(headless=False, location="../../lib/chromedriver.exe") def tearDown(self): self.browser.quit() def test_search(self): self.browser.get("http://www.baidu.com") self.browser.find_element_by_id("kw").clear() self.browser.find_element_by_id("kw").send_keys(u"Webdriver") self.browser.find_element_by_id("su").click() time.sleep(3) assert u"Webdriver" in self.browser.page_source, "页面中不存在要寻找的关键字"