Pop-up windows

hi… i am clicking a link in a page which gives a popup and after closing that popup a new window will be opening and when i try to switch to that window i couldn’t not see the screen shot of the switched window… could you please help me out in switching to the other window which opens after clicking the link and closing the popup

//this is the script i wrote
//loading the page 
$browser.get("http://www.mysmartprice.com/mobile/microsoft-lumia-535-msp5042").then(function(){
  // checking for store_pricetable and store_gostore
  return $browser.findElement($driver.By.className("store_pricetable"));}).then(function(){
  return $browser.findElement($driver.By.className("store_gostore"));}).then(function(){
  // clicking the 'buy from store' link using xpath
  return $browser.findElement($driver.By.xpath('//*[@id="pricetable"]/div[3]/div[5]/a')).click();
});
//for loading the page
$browser.sleep(5000);
//for closing the popup
$browser.findElement($driver.By.className("popup-closebutton")).then(function(element){return element.click();});
$browser.sleep(3000);
//for switching to other which opened after clicking 'buy fro store'
$browser.switchTo().defaultContent();

Hey @siddenki, try window switching with the following:

$browser.getAllWindowHandles().then(function(handles){ 
  $browser.switchTo().window(handles[1]);
});

This should get the second window that’s open. You can switch back then by passing it handles[0]. :smiley:

@bburkholder
sorry i wrote switching into new window but actually its switching into new tab…will it work for switching into new tab when we close the pop-up?
i tried with
$browser.getAllWindowHandles().then(function(handles){
$browser.switchTo().window(handles[1]);
});
but i am getting an error

1 Like

@siddenki aah, sorry. I neglected to mention that I also fixed the step prior to the last where you click the close button. You’re actually clicking the div, and not the link. This is causing the popup to close, and no new window to be invoked. Your final script should look like this:

//this is the script i wrote
//loading the page 
$browser.get("http://www.mysmartprice.com/mobile/microsoft-lumia-535-msp5042").then(function(){
  // checking for store_pricetable and store_gostore
  return $browser.findElement($driver.By.className("store_pricetable"));}).then(function(){
  return $browser.findElement($driver.By.className("store_gostore"));}).then(function(){
  // clicking the 'buy from store' link using xpath
  return $browser.findElement($driver.By.xpath('//*[@id="pricetable"]/div[3]/div[5]/a')).click();
});
//for loading the page
$browser.sleep(5000);
//for closing the popup
$browser.findElement($driver.By.css(".popup-closebutton a")).then(function(element){return element.click();});
$browser.sleep(3000);
//for switching to other which opened after clicking 'buy fro store'
$browser.getAllWindowHandles().then(function(handles){ 
  $browser.switchTo().window(handles[1]);
}); 

@bburkholder thanks for the help :slight_smile: