Python selenium and chromedriver, chrome page doesn't load when minimised - python

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')

Related

Is it possible to use Selenium in already opened Chrome?

This code currently used to manipulate Chrome..
option = Options()
option.add_argument("--incognito")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
But this code makes to open a new chrome window.
What I want is to manipulate the chrome that's already open, is it possible?
Somebody say, Using Chrome in debug mode is answer, but I don't think that's a good choice.
I want to open chrome without any control.

Why is a web page opened through selenium different from a normal browser?

I'm learning how selenium crawls data, but I find that when a website opens through selenium, it's different from what I used to get when I used other normal browsers. Even I add headers. And I'm very confused.
I really want to upload two contrast pictures, but I can't upload them in stackoverflow at present. I even tried to open the chrome driver and enter the web address manually, but the result is still different.
I use Python 3.6, selenium and chrome 75.0.3770.80
from selenium import webdriver
driver = webdriver.Chrome() #创建driver实例
url = 'https://www.free-ss.ooo'
driver.get(url)
At present, I can't post pictures on stack overflow, but I just want to figure out how I can use selenium to get normal web pages.
Aha,I found the problem, really because the target site detected selenium, the solution is to add options
Chrome_options. add_experiment_option ('excludeSwitches', ['enable-automation'])
Faced same issue and was able to resolve it by removing or fixing appropriate user-agent argument and it worked fine in both headless and non-headless mode.
The resolution was inspired by PDHide post

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" });

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