问题描述
- win7下python使用selenium 定位后截取图片问题
-
定位后用imageGrab对img元素截图,代码如下:
checkcodeimg = browser.find_element_by_xpath("//img[@id='randimg']")
x1 = checkcodeimg.location['x']
y1 = checkcodeimg.location['y']
x2 = x1 + checkcodeimg.size['width']
y2 = y1 + checkcodeimg.size['height']
im=ImageGrab.grab(bbox=bbox)
im.save("H:/1.jpg")问题是截出来的图片位置对不上
在java下按这个原理做是ok的 不知哪里出了问题
解决方案
搞明白了,ImageGrab.grab截的是相对整个屏幕的,selenium取的是相对于浏览器的网页的坐标,应该使用selenium先把整图截出来,
再使用Image.crop切一下。
browser.save_screenshot(savepath)
im = Image.open(savepath)
im = im.crop(bbox);
时间: 2024-07-30 13:20:50