Is it possible to use Selenium in already opened Chrome? - python

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.

Related

Automate webpage that just work properly on IE using selenium on Chrome

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.

How to add extension in incognito mode when launched from selenium chrome webdriver with python

We are doing some testing using selenium python webdriver where we need to open one url in incognito mode and enable one extension already installed and then do some actions.
My findings:
loading of extension in incognito mode not working
extension getting loaded when icgnito mode turned off
Verified so many post on stack overflow, nothing worked.
tried below code"
path = os.path.dirname(r"C:\Users\ab\AppData\Local\Google\Chrome\User Data\Default\Extensions\jfpmbokkdeapjommajdfmmheiiakdlgo\0.1.7_0\manifest.json")
options = webdriver.ChromeOptions()
options.add_argument('--incognito')
options.add_argument("--load-extension={path}")
driver = webdriver.Chrome(chrome_options=options, executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.maximize_window()
driver.get(xxxxxxxx)
which throwing error cannot load manifest.json either missing or not readable. However i have made sure the path is correct.
any suggestion please how to load extension while opening chrome driver in incognito mode ?
Rather you loading the required cookies/extension as part of your chrome options, other option is using the chrome profile. Check my answer in this post
To more on the profiles and how they work refer here
Here is the logic to turn on the extension in the incognito mode.
Python:
# driver.get("chrome://extensions/?id=extion_name_goes_here"); # <=== general snippet see below example
# driver.get("chrome://extensions/?id=jfpmbokkdeapjommajdfmmheiiakdlgo")
# select allow in incognito mode checkbox
driver.execute_script("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");
Refer to my answer in this post for more information on the js used above.

Using a headless Chrome instance as an OBS source

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?

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

ChromeDriver/Selenium take a black screenshot when in a separate tab

I'm currently writing a visual test that require adblock to be installed to stop some ads from ruining the test. However when I run the Chromedriver with the adblock extension installed it brings up the successfully installed adblock tab. This tab screws up my screenshots because the driver is working in a previous tab that opens. Is there any way to stop the adblock tab from opening or prevent the black screenshot from happening? Here is the initialization for the driver.
def setUp(self):
chrome_options = Options()
chrome_options.add_extension('AdBlock_v2.6.4.crx')
self.driver = webdriver.Chrome('chromedriver', port=0, chrome_options=chrome_options)
self.driver.implicitly_wait(15)
handles = self.driver.window_handles
self.driver.switch_to_window(handles[-1])
seems to be the best way to handle it. It simply tells the driver to switch to the tab which is considered a window. I feel silly for trying to find a more complex solution

Categories