Divya Mani
Divya Mani

Reputation: 313

take a screenshot with url bar using python selenium

I want to take a screenshot of the Chrome browser including the URL bar using Python+Selenium, but I couldn't find any correct solution.

My code:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.google.com/')
driver.save_screenshot(r'LoadURL.png')

Expected output: google

Actual output: enter image description here

As you can see, the screenshot doesn't include the URL bar. What should I need to add here to fix this?

Upvotes: 4

Views: 2048

Answers (1)

Milan Radojevic
Milan Radojevic

Reputation: 31

You need root for this. You need to install Xvfb for screen and you need to enable screen using "Xvfb :1 -screen 1 1600x1200x16 &" command where ":1" will be ID of the screen that will be input in the script on foolowing line: "os.environ['DISPLAY'] = ':1'"

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from pyvirtualdisplay import Display
import os

os.environ['DISPLAY'] = ':1'

display = Display(visible=0, size=(800, 600))
display.start()

chrome_options = Options()
chrome_options.add_argument('--no-sandbox') #bypass OS security model
chrome_options.add_argument("disable-infobars");
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(executable_path='/root/chromedriver', chrome_options=chrome_options)
time.sleep(3)
driver.get("https://ipinfo.io/json")
driver.maximize_window()
os.system('import -window root screenshot.png')
driver.close()

Output will be screenshot.png with full chrome browser. Happy screenshooting!

Upvotes: 3

Related Questions