Ale*_*der 7 python selenium-webdriver
我想以高分辨率捕获网站的屏幕截图以识别文本或只是为了保存高质量的图像。我在 Python 2.7 中尝试了这段代码。网站http://www.flaticon.com/仅作为示例。
from selenium import webdriver
import time
driver = webdriver.PhantomJS()
#Setting large window size doesn`t resolve the problem
driver.set_window_size(16000, 12000)
driver.get('http://www.flaticon.com/')
time.sleep(3)
#set resolution 640 dots per inch for this image
#???
driver.save_screenshot('./downloaded/img/welcome_icons.png') # save a screenshot to disk
driver.close()
Run Code Online (Sandbox Code Playgroud)
它捕获屏幕截图,但分辨率对我来说还不够。扩大窗口大小并不能解决问题。来自webside的图片仅存在于图像的一部分。似乎图像分辨率不受影响。有没有办法在保存之前明确设置图像分辨率?
如果是关于改变窗口大小,可以通过以下方式设置
driver.set_window_size(480, 320)
Run Code Online (Sandbox Code Playgroud)
以下是一位开发人员在Github上执行此操作的示例。如您所见,您可以调整窗口大小和屏幕截图的质量。
import StringIO
from selenium import webdriver
from PIL import Image
# Install instructions
#
# npm install phantomjs
# sudo apt-get install libjpeg-dev
# pip install selenium pillow
driver = webdriver.PhantomJS(executable_path="node_modules/phantomjs/bin/phantomjs")
driver.set_window_size(1366, 728) # optional
driver.get('http://google.com')
driver.save_screenshot('screen_hires.png')
screen = driver.get_screenshot_as_png()
# Crop it back to the window size (it may be taller)
box = (0, 0, 1366, 728)
im = Image.open(StringIO.StringIO(screen))
region = im.crop(box)
region.save('screen_lores.jpg', 'JPEG', optimize=True, quality=95)
Run Code Online (Sandbox Code Playgroud)
100 的质量为最大,0 为最小。
编辑:
您也可以使用selenium.windowMaxmize()
.
如果您想放大屏幕以查看您所说的某些特定文本,您可以在 Mozilla 中尝试以下操作:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
br = webdriver.Firefox()
zoom = ActionChains(br)
body = br.find_element_by_tag_name('body')
for i in range(2):
zoom.send_keys_to_element(body,Keys.CONTROL,"+").perform()
Run Code Online (Sandbox Code Playgroud)
这有点hacky,但我通过将窗口大小宽度增加到3000并将缩放比例增加到250%为自己解决了这个问题。
driver.set_window_size(3000,800)
driver.execute_script("document.body.style.zoom='250%'")
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
我遇到了完全相同的问题,使用 Python 3.7 和 chrome webdriver,上面的答案有很大帮助,但没有一个对我来说很完美。这是我找到的解决方案:
from selenium import webdriver
from PIL import Image
from io import BytesIO
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# MUST BE HEADLESS AND HAVE VERY LARGE WINDOW SIZE
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=6000x5000")
def get_screenshot(url_path, object_type, object_id):
chrome = webdriver.Chrome(chrome_options=chrome_options,
executable_path='/path/to/webdriver')
chrome.get('http://website.com' + url_path)
chrome.execute_script("document.body.style.zoom = '300%'") # ZOOM
element = chrome.find_element_by_id(object_id) # find part of the page you want image of
location = element.location
size = element.size
png = chrome.get_screenshot_as_png() # saves screenshot of entire page
chrome.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x'] * 3 # must mutliply all these numbers by your zoom
top = location['y'] * 3
right = (location['x'] + size['width']) * 3
bottom = (location['y'] + size['height']) * 3
im = im.crop((left, top, right, bottom)) # defines crop points
im.save(f'{object_id}.png') # saves new cropped image
Run Code Online (Sandbox Code Playgroud)