Set browser locale in headless chrome with python - python

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.

Related

How To Work With Webdriver Without Opening The Browser (Edge) In Python?

Well, I am working on a project that uses selenium to grab some movie files and download them from the website. Everything is working fine but I don't want to open the browser window and want it to run in the background and show the results in the end without invoking the browser. I have tried many ways but have failed to find out a reliable answer. There are questions related to this on StackOverflow but they are not working for Edge. The answers to other questions tell about adding this line.
from selenium.webdriver.Edge.options import Options
options = Options()
options.headless = True
driver = webdriver.Edge(executable_path="msedgedriver.exe", options=options)
But this is not working for me and the following error pop-up.
File "movies.pyx", line 11, in init movies
driver = webdriver.Edge(executable_path="msedgedriver.exe", options=options)
TypeError: __init__() got an unexpected keyword argument 'options'
I also have tried seeing many answers to similar questions but nothing works. If anyone knows do answer i will be very thankful to you in advance.
Well I am using python 3.7.7
It's not directly a solution to the problem, but an alternative, I think you can check this simple library : https://pypi.org/project/activesoup/
activesoup combines familiar python web capabilities for convenient
headless “browsing” functionality.
[...] Consider using activesoup when: [...] You need to actively interact
with some web-page from Python (e.g. submitting forms, downloading
files)
you are using Microsoft Edge webdriver.
the way you are trying make it headless is for chrome webdriver.
options = EdgeOptions()
options.use_chromium = True
options.add_argument("headless")
options.add_argument("disable-gpu")
Try above code by importing:
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
note : you have to enable chromium to enable headless.

Ways to share your screen in an automated script with chrome driver

Using Python 3.7 and the latest chrome driver.
I'm trying to use google meet to share my screen using Selenium's chomedriver, but I keep getting the error "your browser can't share your screen", no matter of the options I give the driver.
I tried disabling gpu and hardware accelerations as I saw in similar questions but nothing changes.
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--start-maximized")
browser = webdriver.Chrome(options=options)
I expect there is a way to automatically share my screen with a script.
After submmitting a bug report at the chromedriver devs they answered a solution.
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2905
By johnchen:
Some Chrome features don't work properly in a test environment. For
this particular case, you can work around by removing the switch that
tells Chrome it's running a test. If you're using Python, you need to
add:
opt = webdriver.ChromeOptions()
opt.add_experimental_option('excludeSwitches', ['test-type'])
In Java, you can use
ChromeOptions opt = new ChromeOptions();
opt.setExperimentalOption("excludeSwitches", new String[] { "test-type" });

ChromeOption '--safebrowsing-disable-download-protection' doesn't disables the download warning in Chrome version 67.x

I am trying to use Selenium webdriver to automate some of the work. My automation includes downloading some .msg outlook email file from the web attached by somebody else. Downloading the .msg file prompted a warning from Chrome saying "This type of file can harm the computer...". Using the ChromeOptions to add argument --safebrowsing-disable-download-protection does not work, the download still prompted the warning with the argument added into the chrome options, any help will be appreciated.
Code trial:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--safebrowsing-disable-download-protection')
driver = webdriver.Chrome(chrome_options=chrome_options)
printing the chrome_options.arguments shows that the '--safebrowsing-disable-download-protection' is added into the arguments, but when I started to download the .msg files using Selenium, I still receive the same warning.
Something to note, when i manually run chrome.exe via cmd using the '--safebrowsing-disable-download-protection', downloading without warning works.
As per your code trials as you are trying to implement --safebrowsing-disable-download-protection through ChromeOptions() but it is worth to mention the following points:
As per Remove kSbDisableDownloadProtection flag to make download safebrowsing protect a default behavior --safebrowsing-disable-download-protection is supposed to be cleaned up as a command flag to make download safebrowsing protect a default behavior.
The fix was dependent on Replace safe browsing DB and update protocol with Pver4 which was marked as fixed as no issues have been reported since full launch.
Subsequently Remove kSbDisableDownloadProtection flag to make download safebrowsing protect a default behavior was also marked as fixed.
The fix Enable PVer4 by default for desktop platforms establishes the fact that the ChromeOption --safebrowsing-disable-download-protection is no more effective.
Conclusion
As per the points mentioned above the ChromeOption --safebrowsing-disable-download-protection is no more an effective/valid ChromeOption and should be handled by PVer4 by default for desktop platforms.
You can try this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\downloads",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False
})
driver = webdriver.Chrome(chrome_options=chrome_options)
This should work
driver = webdriver.Chrome(chromeDriver, options=options)
params = {'behavior' : 'allow', 'downloadPath':r"C:\Users\downloads"}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)

Set download directory using selenium (and python)

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

Creating a headless Chrome instance in Python

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.

Categories