I am learning Selenium-Webdriver and so for practice working on one scenario, but I'm stuck in step#3. Scenario is as follows:
- Open google homepage and perform some search, say for the word "WebDriver".
- Open the first two links in new tabs of the same window.
- Navigate to the second and third tab and get their titles
- Close the tabs and switch back to the google result tab.
So far, I'm able to open google home page, perform a search on the word "WebDriver" and open the first two links, but now I'm unable to switch to the second and third tab and close them. My code so far is:
String originalHandle = driver.getWindowHandle();
System.out.println("Before switching title is:" +driver.getTitle());
String selectLinkOpeninNewTab = Keys.chord(Keys.COMMAND,Keys.ENTER);
WebElement link1 = driver.findElement(By.xpath(".//*[@id='rso']/div[2]/div[1]/div/h3/a"));
link1.sendKeys(selectLinkOpeninNewTab);
WebElement link2 = driver.findElement(By.xpath(".//*[@id='rso']/div[2]/div[2]/div/h3/a"));
link2.sendKeys(selectLinkOpeninNewTab);
Set<String> s1 = driver.getWindowHandles();
Iterator<String> i1 = s1.iterator();
int i = 0;
while(i1.hasNext())
{
i++;
String childwindow = i1.next();
if(!originalHandle.equalsIgnoreCase(childwindow))
{
driver.switchTo().window(childwindow);
Thread.sleep(10000);
System.out.println("After switching title of new Tab "+i+ " title is " +driver.getTitle());
driver.close();
}
}
driver.switchTo().window(originalHandle);
System.out.println("Original window tab title is" +driver.getTitle() );
I'm not sure where it's going wrong and how to fix it. :(