Webdriver API(4)获取 页面元素的文本内容

根据不同浏览器,geckodriver存放位置,代码有改动

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_getElementText(self):
        url="https://www.baidu.com"
        self.driver.get(url)
        import time
        time.sleep(3)
        aElement=self.driver.find_element_by_xpath("//*[@class='mnav'][1]")
        a_text=aElement.text
        self.assertEqual(a_text,u"新闻")#断言格式?
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
    unittest.main()

你可能感兴趣的:(单元测试,自动化测试)