new tab ID in chrome - google-chrome

I want to know the ID which the original new tab page of chrome would take when I open a new tab?
to inject a javascript code into it.
** note that the new tab I'm talking about here is the original chrome new tab page.

This is not possible, JavaScript injection into Chrome's original New Tab Page is not allowed for security reasons.

Related

How to open new tab in browser (in same browser window) with TestCafe?

I am currently unable to open a new tab in the same browser window. Please refer scenario.
After login, the application opens the page in the same browser
window. I need to click on the card and after it will open in a new
tab and where I need to test everything. Currently, I need to
simulate a direct URL for a new tab but I want to open a new tab with
test case code.
Please let me know is it possible in the test cafe?
Thanks.
Yes, you can pass --disableNewWindow in testcafe/runner argument to open url in same tab.
Also, refer to https://testcafe.io/documentation/402841/guides/advanced-guides/multiple-browser-windows#disable-support-for-multiple-windows
--disable-multiple-windows

When I open new tab I read "chrome://new tab"

Tomorrow when I open new tab I read "chrome://new tab". What is that? is dangerous? How can I remove this?
Anything that starts with "chrome://" comes from the browser itself rather than another website. So don't worry about it it shouldn't bother you at all.
The url "chrome://newtab" means a new tab opened by chrome itself.
It is secure and configurable which can be modified in Settings -> On startup.

How to open new window instead of new tab in chrome using webdriver?

In order to automate my test application, I need to open few links in a new window instead of tab. Keep this in mind that I am not opening the links in new tab explicitly, it is my web application which automatically lands user in the new tab after clicking on the link.
Why do I want to do this?
Because running the tests on chrome browser closes the main tab and keeps open the newly opened tab. Which ultimately fails the tests.So ultimate intention is to open the new window instead of tab and handle it properly using driver.getWindowHandles().
What have I done so far?
I tried to find some kind of capability setting or profile in Chrome which automatically opens the links in a new window which are supposed to be open in a tab.But did not find any convincing solution most of the suggestions are CTRL+CLICK ON THE LINK.
I'm not a guru of web-design, but I can suggest following scenario:
// Get required page
// Excecute below JavaScript with JavaScriptExecutor
var reference = document.querySelector('a#someID').getAttribute('href'); // You can use your specific CSS Selector instead of "a#someID"
document.querySelector('a#someID').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#someID').removeAttribute('href')
// Find target link
// Click on it
This code should allow you to make changes in HTML source code of target web-element to force its opening in new browser window.
Note that with this code element's appearance on page will be changed until page refresh
P.S. You didn't mentioned your programming language, so there is no complete implementation... However, this is Python implementation example:
from selenium import webdriver as web
dr = web.Chrome()
dr.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')
dr.execute_script("""
var reference = document.querySelector('a#ftrTerms').getAttribute('href');
document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#ftrTerms').removeAttribute('href')
""")
link = dr.find_element_by_id('ftrTerms')
link.click()
Well, in the absence of any flag/setting/capability in Chrome browser which opens the links in a new window instead of the new tab I used a Chrome Extension for that via WebDriver.
Why did I do that?
Because my tests are running fine on Firefox and I have no idea how many WebElements are there in the suite which gets open in new tab in Chrome browser. The suite is also very huge so doing any changes in its core page class may break the all the tests.In addition to that, changing code at an element level will be very time-consuming and most importantly not a generic solution.
What did I do?
I used a chrome extension New Tab New Window, which opens all the new tabs into a new window.
Downloaded the CRX file of this extension using an extension Get CRX.
Set the CRX file as a capability of Chrome.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("pathOfCRXFile"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
So above will convert all the new tabs into a new window.So whenever the driver clicks on any link which further opens in a new tab will get opened into the new window.

When should links open in new tab/window?

When exactly links should open in a new tab/window? I find that some of the actions which require certain operations in a new tab/window can be done on the same page using modal windows and then refreshing the same page to show the updated contents.
A couple of google searches gave me the following results:
Why external links should open in new tabs?
When if ever should links be opened in a new window?
Is there ever a good reason to force opening a new browser window?
Should links open in new windows?
Forcing links to open in new windows: an argument that should have ended 15 years ago
Some suggest that in case of external links they should be opened in a new tab/window, some suggest that they should be opened on the same page for simplified user control. Both of them sound correct in their own ways. Isn't there some sort of generalization? Or are there any particular situations where we cannot do without links opening in a new tab/window?
In other words, what are the situations where a link should open in a new tab/window OR in the same page (taking into account that modal windows are implemented frequently these days)?
Having external links opened in a new tab is better and done more often so that when you have a user surfing your site and clicks on an external link he doesn't have to go back but simply just needs to close that tab and can continuous surfing your site.
You have to bare in mind that not ever internet user knows how to work with a browser like you do (Still many lesser-intelligent people on the net).
add attribute target="_blank" on a tag
Link 1</strong></p>

Chrome extension, focus on the page instead of omnibox after selected tab is updated?

I created an extension called quickmarks which will open bookmark by keyword at currently selected tab. I am
using omnibox to select the bookmark (chrome.omnibox.onInputEntered),
and chrome.tabs.update API to open bookmark's url in current tab, by
providing the url in updateProperties. However after the tab is
updated, focus still remains in omnibox, which make the user-
experience not as good as I desired. So is there a way to set the
focus to the page, instead of the omnibox.
Btw, I have tried to open a new tab by using chrome.tabs.create. The
page will be focused instead of omnibox, which is my desired
behaviour.
Thanks.
Using chrome.tabs.update(tab.id, {url: url, selected: true}); does the trick for me.
The accepted answer no longer works in Chrome 31. I had hoped it was a simple matter of the selected property being deprecated but the replacement highlighted property did not assign focus to the tab's content either.
I was only able to steal focus from the Omnibox by closing the current tab and then creating a new tab in its place. Here's the code that works in Chrome 31:
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id, function() {
chrome.tabs.create({url:url, active:true});
});
});
While this is certainly not ideal, the current tab is closed and a new one opened so fast you barely notice any difference.
Dave Teare is right that this no longer works in current version of Chrome, however his method did not work for me. It seemed like the chrome.tabs.create did not get called after the tab was removed.
I use the Chrome extension iChrome and I wanted it to be selected when I created a new tab, so I installed another extension that redirects new tabs to iChrome. It unfortunately used the same deprecated "selected:true" method.
From what I can tell there is currently no way to do this cleanly, you cannot have Chrome make the updated tab's input focused nor can you have it select the text in onmibar so you cant just start searching after creating a new tab. So this is what I came up with:
chrome.tabs.create({url:url, active:true}, function(){
chrome.tabs.remove(tab.id);
});
Definitely still not ideal, and you can see a flash when it closes old tab, but it works. The input is focused.