Unable to switch to newly opened tab in chrome using selenium - google-chrome

I opened a new tab from a link in current page. The tab opened but the focus is not shifted to that tab, nor am I able to switch tab using the following two methods I used. I'm using Chrome.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
BaseClass.driver.findElement(By.xpath(xpathOfLinkToPage2)).sendKeys(selectLinkOpeninNewTab);
//method one
ArrayList<String> tabs = new ArrayList<String>(BaseClass.driver.getWindowHandles());
BaseClass.driver.switchTo().window(tabs.get(1));
//method two
String selectLinkOpeninNewTab2 = Keys.chord(Keys.CONTROL,Keys.TAB);
BaseClass.driver.findElement(By.cssSelector("body")).sendKeys(selectLinkOpeninNewTab2);

// open Site 1
String site_1_Window= driver.getWindowHandle();
System.out.println(site_1_Window);
// open Site 2
Set site_Windows= driver.getWindowHandles();
System.out.println(site_Windows);
for (String site_2_Window: driver.getWindowHandles())
{
System.out.println(site_2_Window);
driver.switchTo().window(site_2_Window);
}

try using:
driver.SwitchTo().Window(driver.WindowHandles.Last());
also see this: http://www.binaryclips.com/2016/03/selenium-webdriver-in-c-switch-to-new.html and this Selenium webdriver selecting new window c#

//count and enter your tab index beside get
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(newTab.get(2));

Related

How to get a div which is not a child of document

code below
var n = document.createElement("div");
Object.defineProperty(n, "id", {
get: function() {
window.location.href = homepage
}
})
I want to debug a page on a site, but when developer tools opened,
the code will bring me to homepage.(function get executed in Chrome)
How to get the div, then remove it to avoid jumping
Open the script in developer tools > Source (May be from homepage) & place debugger on the var n = document.createElement("div") line.
Now navigate to the page which you want to debug, the debugger will get activated.
Now replace the line (Or override the function Object.defineProperty(n, "id", .. ) and move step or disable debugger.

Titanium tabs accumulating when opening new windows

I have a list of courses in rows like this:
Whenever I click a row, a new tab is created, and a new window is added to that tab showing the course info.
Then if I press back, it goes back to the courses window, which is great, but when I click another course it adds that to the list of tabs, so it starts looking like this:
Whereas, there should only be two tabs here, the Courses tab and Get Courses tab.
In get_courses.js (the file that deals with making the rows) I have this event listener which creates a new tab every time a row is clicked (which I'm sure is where my mistake is, I'm just not sure how to fix it):
table.addEventListener("click",function(e){
var courseInfo_window = Titanium.UI.createWindow({
title:e.rowData.title,
url:'get_courseInfo.js',
courseIMISCode: e.rowData.courseIMISCode
});
var courseInfo_tab = Titanium.UI.createTab({
title:'Course Info',
window:courseInfo_window
});
Titanium.UI.currentTabGroup.addTab(courseInfo_tab);
});
Which I want to be there to create a Course Info tab, but then in get_courseInfo.js I have this, possibly redundant code:
Ti.UI.currentTabGroup.activeTab.open(courseInfo_window);
Which, in my noob mind seems necessary to open my courseInfo_window, but is accumulating the tabs in the bottom (as shown in the image earlier).
TL;DR: What do I need to do (probably in get_courses.js) to update the Course Info tab instead of opening a new tab for each row click?
You can access tabs in TabGroup through tabs property. However, it would be easier to keep reference to tab which you created outside of event listener and modify inside:
var courseInfo_tab = null;
table.addEventListener("click",function(e){
var courseInfo_window = Titanium.UI.createWindow({
title:e.rowData.title,
url:'get_courseInfo.js',
courseIMISCode: e.rowData.courseIMISCode
});
if (courseInfo_tab === null) {
courseInfo_tab = Titanium.UI.createTab({
title:'Course Info',
window:courseInfo_window
});
Titanium.UI.currentTabGroup.addTab(courseInfo_tab);
} else {
courseInfo_tab.window = courseInfo_window;
}
});

how to search a name of text-file c# windows phone

I'm a newbie wp dev. I want to create a many text file name like 1.text 2.text 3.text ......1760.text and I want user to type the number in text box then click the button and the result is read that typed number.text. how can I do it ? please help
you can try this method:
1. put these txt-files into a path,like:TxtFiles/1.txt,2.txt...
2. when user type the number and click button, execute a method to combine file-path and read the file. and you should check the number first.
StreamResourceInfo resourceInfo = Application.GetResourceStream(new Uri(string.Format("yourFilePath/{0}.txt",userTypedNumber), UriKind.Relative));
using (StreamReader reader = new StreamReader(resourceInfo.Stream))
{
yourContentControl.Text = reader.ReadToEnd();
reader.Close();
}

Is that any option for search tabs in chrome?

that is we have opened many tabs.In that tabs i want to search specific tab. Please tell if any ext or option or add-on in chrome or firefox.
Firefox has this functionality built in. If you just start typing in the URL bar and the first character you type is % followed by a space, the rest of what you type will be treated as a search on the titles and urls of open tabs in all Firefox windows.
I'm not sure if this is the site to be asking for help finding extensions that do end user tasks such as this so I'll answer your question explicitly as well as explain how to do it programatically.
The short answer is, yes one extension that will allow you to do this can be found here:
Tab Title Search
The long answer is, in order to find all tabs with a certain name, you need to use the chrome tabs API
I whipped up a short piece of javascript to demonstrate how to have an extension that will create a popup with a search box that you type the desired tab title into. If the tab is found, it will be listed below the search box. If you click on the listing, you will switch to the tab.
// Function to search for tabs
function searchtabs() {
chrome.tabs.query({
title: ""
},
// Callback to process results
function(results) {
// Place holder for the tab to process
var foundTab = null;
// Text to match against
var queryText = document.getElementById("textToSearchInput").value;
// Div to place divs of matched title in
var queryAnswerDiv = document.getElementById("foundTabsDiv");
// Clear the current children
while (queryAnswerDiv.hasChildNodes()) {
queryAnswerDiv.removeChild(queryAnswerDiv.lastChild);
}
// Iterate over all the results
for (var i = 0; i < results.length; i++) {
// Keep track of the tab that is currently being processed
foundTab = results[i];
// If we have a title containing our string...
if (foundTab.title.indexOf(queryText) > -1) {
// Create a new div
var tabDiv = document.createElement("div");
// Set its content to the tabs title
tabDiv.innerHTML = foundTab.title;
// Let it know what the tabs id is
tabDiv.tabToSwitchTo = results[i].id;
// Allow for users to click on the representing div to switch to it
tabDiv.onclick = function() {
// Make the tab selected
chrome.tabs.update(this.tabToSwitchTo, {
selected: true
});
};
// Append the created div to our answer div
queryAnswerDiv.appendChild(tabDiv);
}
}
});
}
document.addEventListener('DOMContentLoaded', function() {
var inputField = document.getElementById("textToSearchInput");
inputField.focus();
inputField.onkeydown = searchtabs;
});
Also, if this is more what you are looking for rather than the extension that I linked, let me know and I can pack this extension.
Edit:
Fixed an error in using the wrong ID to get the input field as well as not getting the first letter of the title (use indexOf() > -1)
An extension that does this is Tab Hero for Chrome ($0.99 Chrome extension). It searches through all of the open tabs (across multiple windows) and offers to switch to the filtered tab. Try and see if it works for you.

Opening javascript links in new tab

(Question1, question2 and question3 looks how to force users open link in new tab)
But in my situation I visit some sites regularly and they have links like this:
<a href='javascript:window.open("/view.php?id=1234","_self")'>Link name</a>
This type of link makes me impossible to open link in new tab with a mouse click. Every time I see these links, I duplicate the tab in Chrome and click link inside the cloned tab. And go back to original tab and continue to surf. Is it possible to open these links in new tab with a chrome extension, js code or something?
You can try one of the links here: http://bit.ly/12dUk4V
. . The problem is that these links can be kind of "about:blank" because they are not specified in the href attribute normally, so it breaks your expected behavior when using ctrl+click, middle click or something alike. Sometimes sites links to "javascript:" pseudo-protocol, sometimes the link is for "#" with a "onclick" trigger... It depends on the situation.
. . For this specific case it's easy enough to write a user script that will rewrite these kind of links, if you're willing to use something like Tampermonkey:
// ==UserScript==
// #name SelfLinks Fixer
// #namespace http://dnun.es./
// #version 0.1
// #description This script rewrites "window.open(..., '_self')" links so that you can click them as you wish.
// #match http://libgen.info/*
// #copyright 2013, http://dnun.es.
// ==/UserScript==
var tRegExp = '^javascript: *'+
'(window\\.)?open\\('+
' *(([\'"])([^\\3]+)\\3) *,'+
' *[\'"]_self[\'"] *'+
'\\) *;? *$';
var fixLinksCheck = new RegExp(tRegExp);
var as = document.getElementsByTagName('a'), i = 0, n = as.length, a;
for (;i<n;i++) { a = as[i];
if (fixLinksCheck.test(a.href)) { //damn you _self link!
a.href = a.href.replace(fixLinksCheck, '$4');
}
}
. . This code "fixes" only the "_self" links by changing them to normal links. You can then click them with middle button, holding ctrl/shift or whatever. It also leave the "_blank" or "_top" links untouched.
Yes, it is possible. All you need is to inject a simple line of JavaScript code in every page. I had done it before in a Firefox extension.
You just need to override window.open method:
var open_= window.open;
window.open = function(url, name, opts) {
if (name === '_self') { name = '_blank'; }
open_(url, '_blank', opts);
};
Complete code on JsFiddle: http://jsfiddle.net/dp4Uz/