How To Hide Chrome Web Driver Icon (Python-Selenium) - python-3.x

UPDATED WITH REASONING
Goal: Run chrome web driver in background without showing an app icon in the dock or application bar. FYI: I can already run chrome headless, but I have not been able to hide or not show the chrome icon in my app bar.
Reason: I am building an application that accesses various websites in parallel and I would not like the chrome app icons to display for the synchronous tasks that are taking place. I want to run these tasks in the background.
Current Code with chrome options that will display headless chrome
chrome_options = Options()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(executable_path=executable_text, chrome_options=chrome_options)
Research
list of chrome options: https://peter.sh/experiments/chromium-command-line-switches/
one of many posts explaining how to do headless chrome: Selenium - chrome Driver fail to start in background (without a start-up window)
How can I hide the chromedriver app icons from displaying? Is there a chrome option I can set in addition to setting the 'headless' chrome option? Is this more of an OS setting that needs to be made to run chromedriver in the background? I am successfully able to run multiple chromedrivers in headless mode but it would be nice to NOT show the chrome icons in the dock or application tray similar to how phantomjs does not launch an app icon.
^In the image above those are the chromedrivers that run in headless mode displaying in my dock and ideally for my program I would not even want to display those icons since it is all background processing anyways.

Update (this worked for me to hide the chromedriver icons at least on my Mac):
I was not able to find a Chrome Option to accomplish hiding the chromedriver icons that appear in the dock. However, I was able to edit the Info.plist file for Chrome as part of my program to do the trick (of hiding chromedriver icons) using the LSBackgroundOnly key.
In the Info.plist for Chrome, programmatically, I entered:
<key>LSBackgroundOnly</key>
<string>1</string>
On my Mac using python3 code I used os.system to execute a terminal command using defaults to enter the LSBackgroundOnly key when the program is executing and then at the end of program execution I delete the LSBackgroundOnly key from the Info.plist.
like this:
1)
defaults write /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly -string '1'
2)
defaults delete /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly
AS INFO: This is tricky because your normal chrome app may start up in background mode if you don't add/delete LSBackgroundOnly properly during program execution. In my experience, worst case scenario you may have to manually remove LSBackgroundOnly from the Info.plist file then restart your computer to get Chrome out of background mode.
I remove the LSBackgroundOnly key from the Info.plist file in my program after program execution because the key only needs to be in the file when the chromedrivers are launched. After that I want to be able to use regular Chrome app without issues of it opening in background mode. But, if you understand your program execution and handle your exceptions, this will definitely work to hide the icons correctly and there will be nothing different in using your regular Chrome app.
Happy hacking.

This is not possible using Selenium. I don't really understand why you want to do this. Is there a real purpose, or it's just you don't want it to show? Even if the Chrome app shows on the dock, in headless mode, it works pretty good.
However, if you want your icon to not appear in the dock when opened, then you need to disable it by modifying the Info.plist file for that application.
In your Info.plist for Chrome, add this
<key>LSUIElement</key>
<true/>
This will prevent your app icon from appearing in dock, when working. I found this out a long time ago and this is the post where I saw it.
PS - I have not tested it for Chrome app. Please test at your own peril.

Related

Switch to window (tab) without bringing browser to front

I am using Python and Selenium to navigate multiple tabs. How can I switch to a different window/tab without bringing the entire browser window to the front and without the selenium window "stealing" the focus of my computer? For example, if I'm typing in the search bar of a normal browser and my script switches tabs, it'll stop typing the search bar and I'll have to reclick the search bar to start typing again.
No headless mode suggestions please.
Changing the browser location off the screen doesn't work because it still steals focus
Minimizing the browser doesn't work because it unminimizes when the switch to command is run
Here is how I'm switching windows:
for handle in driver.window_handles:
driver.switch_to.window(handle)
The answer is NO, this is not possible to do with Selenium.
Selenium is created to simulate actions a user can do via GUI.
As a user you can't do what you are asking about, Selenium can't do it as well.
I think, If you deploy your Automation scripts to a VM (which is there in you Machine), You can continue to start working on your local browser and Your Automation script will run on a VM and will have a independent environment for the same.

Disable automation warning in Safari when using selenium

I Would like to perform tests in safari, using selenium.
Unfortunately, when I try to debug issues using the safari, a notification appears which requires me either disable automation in safari or to quit (attached image).
why I cannot do it as in Chrome & FireFox (for ex: press F12 to see the DOM) ?
Note: I'm using MacOs Catalina with Javascript & Python for selenium
This error message...
...implies that the Safari browser's WebDriver support for developers and automation developers is turned off by default.
Configure Safari to Enable WebDriver Support
As per the documentation in Testing with WebDriver in Safari Safari's WebDriver support for developers is turned off by default. Enabling the WebDriver support depends on your operating system.
In High Sierra and later:
You need to enable safaridriver once.
/usr/bin/safaridriver --enable
If you’re upgrading from a previous macOS release, you may need to use sudo as follows:
sudo /usr/bin/safaridriver --enable
Sierra and earlier:
To make the Develop menu available. Choose Safari > Preferences and on the Advanced tab, select Show Develop menu in menu bar.
Choose Develop > Allow Remote Automation.
Authorize safaridriver to launch the XPC service that hosts the local web server. To permit this, manually run /usr/bin/safaridriver once and follow the authentication prompt.
Unfortunately we don't seem to be able to pause the tests, interact with the browser and then resume the tests afterwards with safaridriver, as we can with geckodriver and chrome webdriver.
The closest option you have is to 'Stop Session' in the pop-up from your screenshot.
From what I can see the 3 options listed are:
Turn off all automation - Stops the test and turns off all the automation settings. This will require you to allow remote automation/enable safaridriver again.
Stop session - this stops the test but leaves the browser open so that you can manually interact with it and use the Web Inspector tools.
Continue session - this closes the pop-up and continues with the test.
So, to summarize for all other (frustrated) developers who come across #Safari and try to automate it.. it is impossible to write automation because of this limitation.
So the options are:
Write in other browser (such as chrome) and verify it works in safari
Manually in other session in safari(manual) locate your locators/paths and then insert to you automation-infra
and to add to complexity - Selenium clicks doesnt work in many places where on chrome/ff/edge DO work.
so, possible solution to that - is to use JS in DOM - but that is also not perfect.
hopefully the situation will improve
This should solve your problem.
warnFromSafari = driver.SwitchTo().Alert();
warnFromSafari.Accept();

chrome extension occurs multiple times in Task Manager

Sometimes my extension stops working (clicking the browser action doesn't do anything, it stops doing its background activites). I'm debugging that problem.
When it stops working, if I click 'background.html' to open the background page, I get a blank white window instead of the background page (none of the panels open). Reloading the extension from 'chrome://extensions' doesn't allow me to get the background page either. I have to completely quit the browser and restart. If I keep the background page open and then the extension stops working, the JS console doesn't work. I can't use the 'Sources' tab to find out what stack frames are executing etc.
I also noticed that the extension occurs multiple times (multiple adjacent rows) in Google Chrome's own task manager. But I'm unable to select them individually. Clicking one row selects them all.
Anybody seen this before?
Could this occur if my extension code gets into an endless loop? I don't see high CPU usage either at the OS level or in Google Chrome's task manager. Same behavior under both Mac and Linux.
FWIW the extension is relatively complex: it uses web workers, 10s of uploads and downloads in the background page.

Detecting Chrome browser exit?

I tried everything but nothing seems to work for me. I tried to detect the closing of the browsing by detecting window closing like the code below but that does not work for me. What am I doing wrong and is there another solution to detect Chrome closing?
chrome.windows.onRemoved.addListener(function(windowId){
alert("Browser exit!");
});
Your general problem is that quitting the browser means that browser can no longer run your script to check if it has quit yet -- it's like trying to ask a person if they've died: the answer (if you get one) is only ever going to be "no".
One solution you might try is a long-running background page with the background permission:
Makes Chrome start up early and and shut down late, so that apps and extensions can have a longer life.
When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.
This permission allows the Chrome to keep running your extension even after all browser windows have closed. Normally, Chrome can only do things when it has windows open, so we run into the problem of Chrome being unable to detect when all windows have closed (as explained above). With the background permission, Chrome retains the ability to run extension code even when all windows have closed.
i'm just looking to get control when my extension exits not the entire browser; my extension uses a text box exclusively, so i set its onblur property to a function and put my exit code there; probably not what you're looking for, but it works for me!
the chrome developer website recommends not using background pages due to "memory and other resources they consume";
i agree a more consistent way to get control on extension exit would be useful; it is an html page that otherwise seems integrated with the dom;
chrome does not provide any API to detect chrome browser is going close. However, we can detect chrome browser opening event inside background page when using chrome extension.
background.js
chrome.runtime.onStartup.addListener(function () {
console.log("extension started: " + Date.now());
});
this event will fire when chrome browser will start. next you can use any trick to identify the chrome browser was exit.

Stop Browser at any spot

I have checked many questions and answers regarding how to stop any browser at any time, but I have not found a solution to my problem.
I have a logout button in my application, and when I press it, it adds some classes to an <a> tag. Is there any key/shortcut to stop the Mozilla/Chrome (any browser) to perform the actions or stay the same page?
What I believe you're looking for is a debugger with breakpoints. Breakpoints allow you to mark certain sections of your code as points where you want the execution of that code to temporarily pause its state until a point in which you are ready to proceed to the next breakpoint.
Modern browsers, such as Google Chrome (or any other WebKit browser like Safari) or Mozilla Firefox with the Firebug Firefox extension have this capability.
Chrome (WebKit Browsers):
You can set breakpoints in your JavaScript in the "Scripts" tab of Chrome (and most WebKit browsers):
From Chrome Developer Tools: Script Breakpoints:
Open the Developer Tools by hitting the Control-Shift-I shortcut
Open Scripts panel and select "script.js" from scripts drop-down
Set breakpoint on line 19 by clicking the line gutter (you can use the Control-G shortcut to reveal a line in a large file)
Move your mouse over this page
You should stop on the breakpoint
Hover over the source code to inspect local and global variables, function arguments etc.
Delete the breakpoint by clicking the blue tag breakpoint indicator
Click the Continue button or hit F8 in Developer Tools window to resume
Firefox with Firebug:
There are countless demonstrations of Firebug Breakpoints and other debugger functionality located on this page.
The functionality is very similar to that of the Developer Tools in WebKit browsers.
Internet Explorer:
Internet Explorer also has built in Developer Tools, which among many other features gives users the ability to set breakpoints and stop execution of the scripts. While the Chrome Debugger is sufficient in many cases, there are certain bugs and cross-browser issues that tend to affect Internet Explorer more so than other browsers.
Thus, the Setting Breakpoints section of the "Debugging Script with the Developer Tools" page will be most helpful when attempting to resolve issues that only affect Internet Explorer.

Resources