0

I want to take screenshot of several links belonging to youtube. I want to take screenshot of the webpage where it will play the video and then take screenshot or take the screenshot without playing but all this has to happen using python or R but they shouldn't open the browser. it should all happen in the backend.

Any help on this will be great.

Thank you in advance.

I have tried opening the link and taking the screenshot using both R and python and it does take the screenshot without opening browser. But the video screenshot is black with an error which I don't want.

code in R library(webshot) webshot("https://www.youtube.com/watch?v=Nym5stAJAt8","test.png")

code in python from selenium import webdriver driver=webdriver.PhantomJS() driver.set_window_size(1120, 550) driver.get("https://www.youtube.com/watch?v=Nym5stAJAt8") driver.save_screenshot("screenshot.png")

the output I get the output I want

  • Selenium is heavy handed, but I’ve had great success in a variety of contexts when nothing simpler works. FYI since it is just a backend you can access in R with Rselenium too. First step in troubleshooting is to make sure the webdriver is actually displaying what you want (ie if the browser session is actually showing black or error then obviously taking a screenshot will also appear as such). – John Colby May 18 '19 at 18:31
  • To get more help, could you at least post the error? – John Colby May 18 '19 at 18:33
  • @JohnColby - I have the attached both the output I get and the output I want. – purvaja patil May 19 '19 at 06:00

2 Answers2

0

You can use headless mode of chrome. Before call driver of chrome, create option object of option then send to driver as argument. Then you can run this code in backend even some environment without any screen include docker container. But be careful to set window size. This will affect your screenshot result.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options() #
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get_screenshot_as_file("capture.png")
Daniel Chang
  • 100
  • 1
  • 4
0

This maybe is related to the following factors:

  1. Run your code from the command line or automatically schedule your code in the background.

  2. The type and driver of video graphic card of your running machine.

For 1) I always can get the right capture pictures if I start my code from windows10 command line. If it is scheduled from the background, it may not be successful.

For 2) I tried to schedule on backend screenshot of my machine's win10 desktop through the libraries 'pywin32' and 'pyscreenshot' of Python. The result: some of my machines worked well, while some failed in all the black screenshots.

James Wang
  • 31
  • 8