1
//Go to a page where link is present 
driver.findElement(By.xpath("//div[@id='LftNav']/ul/li[3]/div/ul/li[3]/a/label"))).click();
//Click this link will open up a new tab.     
driver.findElement(By.xpath("//tr[2]/td[9]/a")).click();

//Now I have to verify a value present in the New Tab.

For that I have to pass the Control to the New Tab. How can I do it?

Kiki
  • 2,243
  • 5
  • 30
  • 43
kripindas
  • 480
  • 2
  • 7
  • 21

3 Answers3

2

You can switch tab (and complete windows) by using following function:

ArrayList<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(windowHandles.get(1));

exept you have only two handles. If you want to close the tab and go back you can use:

driver.close();
driver.switchTo().window(windowHandles.get(0));

If you have more then two tabs you can use:

driver.getWindowHandles().size()

to get the amount of opened tabs and just jump to size()-1

spcial
  • 1,529
  • 18
  • 40
  • Thanks for that answer. But I have a problem now. Actually the tab which has been opened is a pdf which containing data. If the pdf contains no data then am able to verify a text present in the page, but if the pdf page contains some data. Then It is not able to verify that text. Can you please give me a suggestion. – kripindas Aug 06 '15 at 05:48
  • If data is present in the pdf page then the windowhandles option is not working. It is not taking the control to the next page. I checked it but taking the control next tab. – kripindas Aug 06 '15 at 06:07
  • Difficult to say because I haven't worked with PDF files and selenium yet. Actually I don't know if it is even possible to check a PDF file with selenium methods. I just found this: http://blog.wedoqa.com/2014/09/how-to-read-text-from-pdf-file-using-java-and-selenium-webdriver/ – spcial Aug 06 '15 at 06:17
0
String mainWindow = driver.getWindowHandle(); // get the main window handle
driver.findElement(By.cssSelector("//*[#id='someId']")).click(); // click some link that opens a new window

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle);
 // switch focus of WebDriver to the next found window handle (that's your newly opened window)
if(driver.findElement(By.Id("SomeID")).getText().equals("expected window title"))
break;
}
Peanut
  • 3,753
  • 3
  • 31
  • 45
tejal
  • 1
  • 1
0

SendKeys didn't work for me. i tried following code which worked for me:

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
Robot robot = new Robot();
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);  
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);  
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);
int tabNo = driver.getWindowHandles().size();
System.out.println(tabNo);
ArrayList<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(windowHandles.get(tabNo-1)); 
driver.get("http://www.bing.com/");