So here is my issue :
I'm trying to load a page with the browser minimised but it seems like it doesn't work.
I think there might be two reason : RAM management or the website prevent this.
So is there a way to make chrome think that it is on focus while it's not ?
Thanks a lot if you take time to respond to me !
EDIT : after some testing this issue only happens with chrome 87, so I think it's chrome ram management that the issue. The goal is to make chrome believe that I'm actually looking at the window.
You may use the headless option
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
I'm running headless chrome with python script, from docker container. Browser is opening in local language, instead of the language I have specified within the code.
I am using experimental option, but it doesn't work.
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
This doesn't work either:
options.add_argument('--lang=es')
Or this:
browser_locale = 'en'
options.add_argument("--lang={}".format(browser_locale))
Help appreciated.
Eventually, I found the solution to be this:
In my create_driver method, I added self.driver.get("https://www.google.com/ncr") which resolved the issue of opening the browser in the local language.
NCR stands for NoCountryRedirect.
Would it be possible to use an instance of headless Chrome that is created by Selenium in Python3
Here is the code that i'm using to launch headless chrome.
options = Options()
options.set_headless(True)
driver_chrome = webdriver.Chrome(options=options)
driver_chrome.get("http://google.com/")
as a source in the broadcasting software OBS? I know that OBS is able to detect regular instances of non-headless chrome by using Window Capture, but I'm not sure how to apply this to an instance of headless Chrome.
If there is no immediate solution, would one be able to point me in the right direction in what steps to take that would allow this to happen?
I was originally going to ask this question just for Safari. However, I've yet to find the answer for Edge, Opera, Safari and IE (although I think it might not be possible for the latter). Since there seems to be no goto place for this simple question I figured this could all be put into one post.
Questions: Is this possible for Edge, Opera, Safari and IE? If so, how?
Here is the code for Chrome and Firefox for reference
# Chrome
options = selenium.webdriver.ChromeOptions()
options.add_experimental_option("prefs", {"download.default_directory": download_directory})
driver = selenium.webdriver.Chrome(chrome_options=options)
# Firefox
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir", download_directory)
driver = webdriver.Firefox(firefox_profile=profile)
Note that if it's possible via some other language bindings then I'm sure it is via python. So feel free to post non-python solutions and I'll translate once I have the hint!
Internet Explorer doesn't use profiles. It's a limitation of the browser itself, not the IE driver. As such, there is no way to automatically download files to a specified location with Internet Explorer.
and for Safari check this link:how to handle file downlaod for selenium webdriver for safari
This question describes my conclusion after researching available options for creating a headless Chrome instance in Python and asks for confirmation or resources that describe a 'better way'.
From what I've seen it seems that the quickest way to get started with a headless instance of Chrome in a Python application is to use CEF (http://code.google.com/p/chromiumembedded/) with CEFPython (http://code.google.com/p/cefpython/). CEFPython seems premature though, so using it would likely mean further customization before I'm able to load a headless Chrome instance that loads web pages (and required files), resolves a completed DOM and then lets me run arbitrary JS against it from Python.
Have I missed any other projects that are more mature or would make this easier for me?
Any reason you haven't considered Selenium with the Chrome Driver?
http://code.google.com/p/selenium/wiki/ChromeDriver
http://code.google.com/p/selenium/wiki/PythonBindings
This question is 5 years old now and at the time it was a big challenge to run a headless chrome using python, but the good news is:
Starting from version 59, released in June 2017, Chrome comes with a headless driver, meaning we can use it in a non-graphical server environment and run tests without having pages visually rendered etc which saves a lot of time and memory for testing or scraping. Setting Selenium for that is very easy:
(I assume that you have installed selenium and chrome driver):
from selenium import webdriver
#set a headless browser
options = webdriver.ChromeOptions()
options.add_argument('headless')
browser = webdriver.Chrome(chrome_options=options)
and now your chrome will run headlessly, if you take out options from the last line, it will show you the browser.
While I'm the author of CasperJS, I invite you to check out Ghost.py, a webkit web client written in Python.
While it's heavily inspired by CasperJS, it's not based on PhantomJS — it still uses PyQt bindings and Webkit though.
I use this to get the driver:
def get_browser(storage_dir, headless=False):
"""
Get the browser (a "driver").
Parameters
----------
storage_dir : str
headless : bool
Results
-------
browser : selenium webdriver object
"""
# find the path with 'which chromedriver'
path_to_chromedriver = '/usr/local/bin/chromedriver'
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_experimental_option('prefs', {
"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}],
"download": {
"prompt_for_download": False,
"default_directory": storage_dir,
"directory_upgrade": False,
"open_pdf_in_system_reader": False
}
})
browser = webdriver.Chrome(path_to_chromedriver,
chrome_options=chrome_options)
return browser
By switching the headless parameter you can either watch it or not.
casperjs is a headless webkit, but it wouldn't give you python bindings that I know of; it seems command-line oriented, but that doesn't mean you couldn't run it from python in such a way that satisfies what you are after. When you run casperjs, you provide a path to the javascript you want to execute; so you would need to emit that from Python.
But all that aside, I bring up casperjs because it seems to satisfy the lightweight, headless requirement very nicely.