擅长:python、mysql、java
<p>在Firefox驱动程序中没有实现web元素的屏幕截图。解决方法是从屏幕截图中裁剪目标元素:</p>
<pre><code>import StringIO
from selenium import webdriver
from PIL import Image
driver = webdriver.Firefox()
driver.get('http://stackoverflow.com')
# get the logo element
element = driver.find_element_by_id('hlogo')
# crop to the logo from the screenshot
rect = element.rect
points = [rect['x'], rect['y'], rect['x'] + rect['width'], rect['y'] + rect['height']]
with Image.open(StringIO.StringIO(driver.get_screenshot_as_png())) as img :
with img.crop(points) as imgsub :
imgsub.save("c:\\temp\\logo.png", 'PNG')
</code></pre>