Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use --screenshot in headless firefox in Selenium with python

Using @DebanjanB's reply in How to make firefox headless programatically in Selenium with python?, I'm trying to use his code and change it to use --screenshot argument, but it's not working. This is my code

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
print driver.title
driver.quit()
sys.exit()

Can someone let me know please how to use --screenshot with Python and Firefox? Thanks

like image 449
nurub Avatar asked Jan 13 '18 23:01

nurub


People also ask

How do I take a screenshot using Selenium in Python?

Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file.

How do I run Firefox in headless mode?

Firefox in headless mode, can be run once we configure the geckodriver path. We shall then use the FirefoxOptions class, and send the headless knowledge to the browser with setHeadless method and pass true as a parameter to it.

Can we take screenshot in headless mode Selenium?

Screenshots are beneficial, specifically in headless test execution, where you cannot see the GUI of the application. Still, Selenium will capture it by a screenshot and store it in a file so that you can verify the application later.

How do I run Firefox headless mode in Selenium Python?

Headless Execution Firefox Driver It is quite simple to run your tests in the headless mode. You need simply to add the "--headless" argument to the FirefoxOptions object. * However, the current official version of Mozilla Firefox is 56.


1 Answers

Nevermind, I found a way. there is a function driver.save_screenshot('test.png'). I kept the line from my question and commented it out.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import sys

options = Options()
options.add_argument( "--headless" )
# options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
driver.save_screenshot('test.png')
print driver.title
print driver.current_url
driver.quit()
sys.exit()
like image 77
nurub Avatar answered Oct 21 '22 12:10

nurub