0

While automating via Selenium WebDriver, I have the below scenario. On a window, I copy the link and want to open the link on the new window(not in the new tab) and want to set focus on the new window. (Here the second window is not the child window of the first window)please help

3 Answers 3

1

You can do something like this

WebDriver driverOne=new ChromeDriver();  
// navigate to your desired URL
driverOne.get("http://www.yourwebsite.com/");

// Do your stuff and copy the new link
// string newURL;

WebDriver driverSecond=new ChromeDriver(); 
driverSecond.get(newURL);

driverSecond will have a focus on the new window and once your actions are complete close the driverSecond.

Sign up to request clarification or add additional context in comments.

7 Comments

If it's working for you mark this as the answer and upvote
I have tried this but it's not working, WebDriver driver2=new ChromeDriver(); driver2.get(reff); "reff" is my link which is copied from the first window .
Not giving any error, but the focus is on the first window.
Also, I m trying to read an element from the second window which is not able to found
Please provide more details. what type of element, how are you identifying, are using XPath ?.
|
0

In Selenium 4 (currently in beta), you the following will open a new window then automatically switch to the window:

  @Test
  public void openNewWindowForTestProjectBlog () {
    WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
    newWindow.get("https://blog.testproject.io/");
    System.out.println(driver.getTitle());
  }

2 Comments

I m using this solution, but not able to open new window
import org.openqa.selenium.WindowType; WebDriver new1=driver.switchTo().newWindow(WindowType.WINDOW); new1.get(reff); System.out.println(driver.getTitle());
0
//assume - multiple windows are opened by clicking link or a button.    
Set<String> windows = driver.getWindowHandles(); 
    
    for (String window : windows) 
    { 
       if (driver.getTitle().contains("***Something that is on new window***")) 
          { 
             driver.switchTo().window(window);
             //To get title of new window
             System.out.println(driver.switchTo().window(window).getTitle());
          }
    } 

1 Comment

hello my scenario is different, there is not any button or link on the main window where I am clicking on that, here the second window is not a child window, it is a separate window and I want to set focus on the second window

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.