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,280 points
6,349 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,450 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, 2019 by Abha
• 28,000 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,460 points
2,352 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, 2019 in Selenium by Vaishali Walia
3,648 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,520 points
2,254 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
1,386 views
0 votes
2 answers

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,520 points
4,378 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,294 views
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,890 points
3,166 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
3,693 views