Handling new window using Selenium

0 votes

Below is the code:

driver.findElement(By.id("NewButton")).click();
//Thread.sleep(3000);
String new1 = driver.getWindowHandle();
driver.switchTo().window(new1);
driver.findElement(By.id("srcNewCode")).sendKeys("977343");
So, this was the error

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == srcNewCode (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 404 milliseconds.
Please Help????
Apr 2, 2018 in Selenium by code_ninja
• 6,220 points
2,410 views

2 answers to this question.

0 votes

You are not switching to new window. For this save the original window
then get the window handle of the new window and switch to that.After new window work is done ,
close the to and switch to original window handle. Use below code for reference

String parentHandle = driver.getWindowHandle();
driver.findElement(By.xpath("//*[@id='someXpath']")).click();

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle
}

//code for on new window

driver.close(); // close newly opened window
driver.switchTo().window(parentHandle); // switching back to the original window
answered Apr 2, 2018 by DragonLord999
• 8,380 points
0 votes

You can try something like this too: 

 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
 ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
 driver.switchTo().window(tabs.get(1)); //switches to new tab
 driver.get("https://www.facebook.com");
answered Aug 27 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
1 answer

Handling a popup window using selenium

This is a code I use when ...READ MORE

answered Aug 31, 2018 in Selenium by Meci Matt
• 9,420 points
631 views
0 votes
1 answer
0 votes
1 answer

How to apply wait for new window using Selenium WebDriver?

As sometimes during web automation, when you ...READ MORE

answered Jul 9 in Selenium by Vaishali Walia
235 views
0 votes
1 answer

I tried to open a new website when a new tab is opened using Selenium Webdriver

Try this code: ArrayList<String> tabs = new ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
1,079 views
0 votes
1 answer

Handling JavaScript Alert window in Selenium WebDriver

You should try using waits for alerts ...READ MORE

answered Apr 10, 2018 in Selenium by ghost
• 1,790 points
906 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
982 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,140 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
883 views