[英]Take screenshot of full page with Selenium Python with chromedriver
[英]Trying to take and save a screenshot of a specific element (selenium, python, chromedriver)
我正在尝试拍摄并保存可以通过导航到https://www.instagram.com/p/B9MjyquAfkE/看到的图像+评论块的屏幕截图。 下面是我的一段可测试的代码。
我收到一个错误:
article.screenshot_as_png('article.png') TypeError: 'bytes' 对象不可调用
代码似乎能够找到文章,但屏幕截图有问题。 我还尝试指定要在计算机上保存屏幕截图的某个位置。
from selenium import webdriver
import time
class bot:
def __init__(self):
self.driver = webdriver.Chrome("path to chrome driver here")
def screenShot(self):
driver = self.driver
driver.get("https://www.instagram.com/p/B9MjyquAfkE/")
time.sleep(2)
#find post+comments block on page
article = driver.find_elements_by_xpath('//div[@role="dialog" or @id="react-root"]//article')[-1]
#take screenshot of the post+comments block
article.screenshot_as_png('article.png')
if __name__ == "__main__":
bot = bot()
bot.screenShot()
尝试代替
article.screenshot_as_png('article.png')
这个:
screenshot_as_bytes = article.screenshot_as_png
with open('article.png', 'wb') as f:
f.write(screenshot_as_bytes)
解释:
article.screenshot_as_png
已经是以字节为单位的屏幕截图,您需要做的就是保存它。 如果你像article.screenshot_as_png()
一样调用它,那么将尝试在字节上执行,因此错误: TypeError: 'bytes' object is not callable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.