#!usr/bin/env python #-*- coding:utf-8 -*- #在HTML5的画布元素上进行绘画操作 import unittest,time from selenium import webdriver class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_HTML5Canvas(self): url = ‘http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_canvas_line‘ self.driver.get(url) self.driver.execute_script("var c = document.getElementById(‘myCanvas‘);" #getElementById(‘myCanvas‘);获取页面上的画布元素 + "var cxt = c.getContext(‘2d‘);" #var cxt = c.getContext(‘2d‘);设定画布为2d + "cxt.fillStyle = ‘#FF0000‘;" #cxt.fillStyle = ‘#FF0000‘;设定填充色为红色 + ‘cxt.fillRect(0,0,150,150);‘) #cxt.fillRect(0,0,150,150);在画布上绘制矩阵 time.sleep(3) self.driver.save_screenshot("e:\\HTML5Canvas.png") def tearDown(self): self.driver.quit() if __name__ == ‘__main__‘: unittest.mian()