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
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.
7 Comments
Rahul Das
If it's working for you mark this as the answer and upvote
nilima patil
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 .
nilima patil
Not giving any error, but the focus is on the first window.
nilima patil
Also, I m trying to read an element from the second window which is not able to found
Rahul Das
Please provide more details. what type of element, how are you identifying, are using XPath ?.
|
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
nilima patil
I m using this solution, but not able to open new window
nilima patil
import org.openqa.selenium.WindowType; WebDriver new1=driver.switchTo().newWindow(WindowType.WINDOW); new1.get(reff); System.out.println(driver.getTitle());
//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
nilima patil
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