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?
Related
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.
I have a webpage that just work properly on IE, In other browsers I get a messed up layout, but I need to automate it with headless mode, can selenium work on chrome even with the layout messed?
Does this webpage work fine in Chrome? If this is the case, you can implement this requriement via chrome driver with Selenium.
But if this webpage only work correctly in IE, the you can only implement automation in IE or Edge IE mode with Selenium. And Edge IE mode tabs actually running Internet Explorer, so they all use IE driver.
Here is a simple example:
from selenium import webdriver
from selenium.webdriver.ie.service import Service
ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
ser = Service(r"C:\Users\Administrator\Desktop\IEDriverServer.exe")
driver = webdriver.Ie(service = ser, options=ieOptions)
driver.maximize_window()
driver.get('https://www.google.com/')
Note: Please modify the path parameters according to your own situation.
I'm trying to use IE using the selenium module in python.
I want to log in with different login IDs in multiple IE windows, but the login information in IE windows is shared with each other, so independent login is not possible.
I am using selenium version is 3.6 and the explorer version is 11.
How can I fix it?
For example, when I log in to Google email in the first explorer window, when the second explorer window is turned on, I am already logged in to the Google email.
I want to log in to the same site with a different ID at the same time.
IE_1 = ID_1
IE_2 = ID_2
IE_3 = ID_3
....
You can achieve separate logins by running the browser in "private" mode.
This is not specific to IE. You can do this in Chrome Firefox, and other browsers as well.
1. IE in private mode
From the docs, you can set "IE Command-Line Options".
Example below is directly from the documentation.
I have NOT tested the below code myself
as I don't have IE in my environment.
from selenium import webdriver
options = webdriver.IeOptions()
options.add_argument('-private')
options.force_create_process_api = True
driver = webdriver.Ie(options=options) # MIGHT WANT TO CHANGE
# Navigate to url
driver.get("http://www.google.com")
driver.quit()
If you don't have the $PATH set for IE, try this:
driver = webdriver.ie(executable_path="<YOUR_IE_PATH>",
options=options)
2. Chrome in private mode
For Chrome, I can confirm it is valid working code. It's a simplified version of what I use myself.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(
executable_path="<YOUR_CHROME_DRIVER_PATH>",
chrome_options=chrome_options)
driver.get('https://google.com')
For the full list of acceptable "chrome_options", you can check out the reference. This link may not look "official", but it's actually referenced from the Chromedriver website (under "Recognized Capabilities" - "ChromeOptions object" - "args"), so we can safely rely on this doc.
I searched for this question,and I found an idea using driver.switch_to.window(),but it didn't work as expect:
from selenium import webdriver
driver1=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver1.get('https://www.google.com')
driver2=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver2.get('https://www.bing.com/')
driver1.switch_to.window(driver1.current_window_handle)
above code will first open a chrome window and go to google,then will open another chrome window and go to bing,then
driver1.switch_to.window(driver1.current_window_handle)
seems didn't work,the window showing bing still shows on top of the window showing google.
Anyone have any idea?I think
driver1.switch_to.window(driver1.current_window_handle)
may have some BUG.
As you have used two WebDriver instances as driver1 and driver2 respectively to openthe urls https://www.google.com (e.g. windowA) and https://www.bing.com/ (e.g. windowB) it is worth to mention that the function switch_to.window() is a WebDriver method. So, driver1 can control only windowA and driver2 can control only windowB.
For Selenium to interact with any of the Browsing Window, Selenium needs focus. So to iterate among the different Browsing Windows you can shift the focus to the different Browsing Window using JavascriptExecutor as follows :
Python:
driver1.execute_script("window.focus();")
driver2.execute_script("window.focus();")
Java:
((JavascriptExecutor) driver1).executeScript("window.focus();");
((JavascriptExecutor) driver2).executeScript("window.focus();");
I believe you have a different concept of "window" in driver.switch_to.window(). In chrome browser, it means "tab". It's not another chrome browser or browser window like what are you trying to do in your code.
If switch_to.window() what you really want, I'll give an example how to use it:
driver=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver.get('https://www.google.com')
# open a new tab with js
driver.execute_script("window.open('https://www.bing.com')")
driver.switch_to.window(driver.window_handles[-1])
# now your driver is pointed to the "tab" you just opened
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.