简体   繁体   English

Selenium,PhantomJS屏幕截图模糊

[英]Selenium, PhantomJS screenshot is blurry

I am taking a screenshot with phantomjs that is coming out blurry. 我正在使用phantomjs截屏,结果变得模糊。 Is it possible to get better resolution of a screenshot so that it looks like the actual page? 是否可以获得更好的屏幕截图分辨率,使其看起来像实际页面?

driver = webdriver.PhantomJS() 
driver.set_window_size(a, b)
driver.get('www.page.com')
driver.save_screenshot('screenpic.png')   
screen =  driver.get_screenshot_as_png()
im = Image.open(StringIO.StringIO(screen))
#I then set a region to crop called wndw 
region = im.crop(wndw)

How can I make the page sharper? 如何使页面更清晰?

The page gets focus information through the onfocus and onblur callbacks in the window object, which you can call yourself: 该页面通过window对象中的onfocusonblur回调获取焦点信息,您可以称自己为:

browser.execute_script("window.onfocus()")

If a specific element needs to be focused, try either: 如果需要关注特定元素,请尝试以下任一方法:

browser.execute_script("document.getElementById('id').focus()") browser.execute_script("document.getElementById('id').click()") browser.execute_script("document.getElementById('id').focus()") browser.execute_script("document.getElementById('id').click()")

From the comment to @Phillip's answer it seems you don't actually want the entire page, you want to get an image of one SVG file. 从评论到@Phillip的答案,似乎您实际上并不想要整个页面,而是想要获得一个SVG文件的图像。

Why don't you get the SVG file and convert it to an image directly? 为什么不获取SVG文件并将其直接转换为图像? According to this answer you can use Python rsvg to render SVG files. 根据此答案,您可以使用Python rsvg呈现SVG文件。

If you don't know the SVG's URL you can use Selenium to find it out, then download it from Python and render to PNG. 如果您不知道SVG的URL,则可以使用Selenium来找到它,然后从Python下载并渲染到PNG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM