如何使用硒测试画布

时间:2018-06-26 07:11:38

标签: selenium canvas

有什么方法可以使用硒测试画布吗?

例如:

  • 测试画布中是否为红色圆圈
  • 移动动作

1 个答案:

答案 0 :(得分:0)

您可以将Selenium与OpenCV结合使用,如此处所述:

http://hacklabos.org/2019/02/python-html5-canvas-game-web-automation-example-using-selenium-and-opencv/

导入示例:

driver = webdriver.Chrome("./chromedriver")
driver.get("http://www.tripletsandus.com/80s/80s_games/html5_galaga.htm")
…
def get_driver_image(driver):
canvas_base64 = driver.execute_script("return 
arguments[0].toDataURL('image/png').substring(21);", a)
cap = base64.b64decode(canvas_base64)
global image
global gray_image
image = cv2.imdecode(np.frombuffer(cap, np.uint8), 1)

之后,您可以使用OpenCV做魔术,例如:

https://www.pyimagesearch.com/2016/02/15/determining-object-color-with-opencv/