siv
siv

Reputation: 21

Window handles with new Window URL

How does Windows handles in selenium paste a URL from 2nd window to a new window

When I submit all information in this form(https://equallevel.com/punchout/cxml-punchout-tester#advanced) it opens a new window.In new window it redirects to another site where I need to do some user interactions.Here at this point I have Iframe in last redirected site.So I want to get that URL from here and put it in another tab to perform user actions How do I grab the URL(from 2nd window) to a new window from getWindowHandles()

Upvotes: 1

Views: 950

Answers (1)

Manmohan_singh
Manmohan_singh

Reputation: 1804

Refer the below code snippet, It will return the URL to a String variable.

public void switchWindow(String name){
        String URL = null;
        wait.waitForWindowToBeActiveAndSwitchToIt(2);
   System.out.println(driver.getWindowHandles().size());

          for (String current : driver.getWindowHandles()) {
              if(driver.getTitle().equals(name)){
              System.out.println("Switched to window titled =>" +driver.getTitle());
              URL = driver.getCurrentUrl();
              break;
              }

              System.out.println("Switching to window handle:"+ current + "\n title: "+ driver.getTitle());
              driver.switchTo().window(current);
              }
    } 

To open this URL in new tab , do this :

driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");        
string newTabInstance = driver.WindowHandles[driver.WindowHandles.Count-1].ToString();
driver.SwitchTo().Window(newTabInstance);
driver.Navigate().To(URL);

Upvotes: 1

Related Questions