0

I have 1 3rd party integration as Paypal. When I will click on Place Order button it will navigate me from Place Order page to paypal page. Can you please let me know how it will be work. I have tried below code and I will redirect to Paypal Page but new window gets appears instead of same page. Please let me know how I will able to stay on same screen.

String handle= driver.getWindowHandle();
System.out.println(handle);
driver.findElement(By.name("New Message Window")).click();
Set handles = driver.getWindowHandles();
System.out.println(handles);
for (String handle1 : driver.getWindowHandles()) {
    System.out.println(handle1);
    driver.switchTo().window(handle1);
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
nikhil naphade
  • 9
  • 1
  • 1
  • 4
  • The code you wrote will make your selenium driver to switch to the new window which gets opened. What is your expected scenario? – Shivam Mishra Aug 03 '18 at 10:51
  • 2
    Possible duplicate of [How to switch to the new browser window, which opens after click on the button?](https://stackoverflow.com/questions/9588827/how-to-switch-to-the-new-browser-window-which-opens-after-click-on-the-button) – JeffC Aug 03 '18 at 15:19
  • From your question, it sounds like you *DON'T* want to switch to the new window ... if this is the case, you don't have to do anything. Selenium doesn't switch context to the new window unless you tell it to. – JeffC Aug 03 '18 at 15:19

2 Answers2

0

To switch to a new window you need to induce WebDriverWait with ExpectedConditions set as numberOfWindowsToBe() as follows:

String parent_handle= driver.getWindowHandle();
System.out.println(parent_handle);
driver.findElement(By.name("New Message Window")).click();
new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> handles = driver.getWindowHandles();
System.out.println(handles);
for(String handle1:handles)
    if(!parent_handle.equals(handle1))
    {
        driver.switchTo().window(handle1);
        System.out.println(handle1);
    }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • getting an error message for - if(!parent_window.equals(handle1)) – nikhil naphade Aug 03 '18 at 11:08
  • Please check I have updated my code. and Instead By.name I have put Xpath. Check it As I am not able to switch within same tab. It will allows me to redirect on another chrome window. Link for the screenshot: https://i.stack.imgur.com/xw5ue.jpg – nikhil naphade Aug 03 '18 at 11:25
  • @nikhilnaphade This question was about **How to switch to a new window in through Selenium**. Once you have received well researched answers you must not change the original question as per stackoverflow rules. If your requirements have changed feel free to raise a new question. Stackoverflow volunteers will be happy to help you out. As of this occasion I am reverting the question to the initial state. – undetected Selenium Aug 03 '18 at 11:28
  • 2
    Why are you adding an answer to a question that is clearly a dup? – JeffC Aug 03 '18 at 15:19
  • @JeffC The possible duplicate which you have marked is of 2012. Since then _Selenium_ have evolved a lot. Until and unless you update yourself with the recent implementation of _Selenium_ you won't be able to use the new features introduced by _Selenium Team_. In the recent implementation of _Selenium_ as per best practices switching window requires _WebDrierWait_. Hence was my Answer. Let know if you require further help. – undetected Selenium Aug 03 '18 at 19:31
  • @DebanjanB We don't need all the new features implemented in the last 5 years, we only need to switch windows which doesn't require any new code other than what I linked as a dup. – JeffC Aug 03 '18 at 20:51
  • @JeffC Of-coarse you shouldn't expect everyone to believe in the old school thoughts like you. Why don't you let others learn the implementation of `Selenium` as per the current updates? Why do you want to keep the new users in dark? That's total injustice. – undetected Selenium Aug 03 '18 at 20:56
  • @DebanjanB That's not what I was saying... what I was saying was that all the code necessary to answer this question exists in the dup I linked. Your point that a lot has changed in the last five years isn't relevant to this question. – JeffC Aug 03 '18 at 20:57
  • @JeffC Once again, I would suggest you, read the documentation again. You are still dealing with a 5 years old implementation. If you don't update yourself with `Selenium 3.x` you wouldn't be able to make a fruitful transition to `Selenium 4.x` from this October, 2018. Rest is upto you. – undetected Selenium Aug 03 '18 at 21:02
  • @DebanjanB What in the world are you talking about? You really need to work on reading and understanding people's comments. – JeffC Aug 03 '18 at 21:04
0

I have a test environment for selenium using protractor within which there will be a driver indicating the webdriver. I am using the selenium-webdriver npm package in my js code and I have a function that switches between the old and new tab/popup :

goToTab(driver, tab) {
    if (tab !== 1 || tab !== 0) {
      throw new Error(`Tab ${tab} doesn't exist`);
    }
    return driver
      .then(() => driver.getAllWindowHandles())
      .then((handles) => {
        driver.switchTo().window(handles[tab]);
      })
      .catch((err) => {
        console.error('error in goToTab: ' + err);
        throw err;
      });
  }
PersianIronwood
  • 639
  • 8
  • 19