[英]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.