Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can selenium web driver get to know when the new window has opened and then resume its execution

I am facing an issue in automating a web application using selenium web driver.

The webpage has a button which when clicked opens a new window. When I use the following code, it throws OpenQA.Selenium.NoSuchWindowException: No window found

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); //Switch to new window _WebDriver.SwitchTo().Window("new window name"); //Click on button present on the newly opened window _WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

To solve the above issue I add Thread.Sleep(50000); between the button click and SwitchTo statements.

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); Thread.Sleep(50000); //wait //Switch to new window _WebDriver.SwitchTo().Window("new window name"); //Click on button present on the newly opened window _WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

It solved the issue, but I do not want to use the Thread.Sleep(50000); statement because if the window takes more time to open, code can fail and if window opens quickly then it makes the test slow unnecessarily.

Is there any way to know when the window has opened and then the test can resume its execution?

like image 750
Ozone Avatar asked Feb 08 '12 05:02

Ozone


People also ask

How does Selenium handle newly opened windows?

Iterate through child windows. Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

Which method can be used to close a new tab opened by driver in Selenium?

close() method is used to close the current browser window on which the focus is set, on the other hand quit() method essentially calls the driver. dispose method that successively closes all the browser windows and ends the WebDriver session graciously.


2 Answers

You need to switch the control to pop-up window before doing any operations in it. By using this you can solve your problem.

Before opening the popup window get the handle of main window and save it.

String mwh=driver.getWindowHandle();

Now try to open the popup window by performing some action:

driver.findElement(By.xpath("")).click();  Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows  Iterator ite=s.iterator();  while(ite.hasNext()) {     String popupHandle=ite.next().toString();     if(!popupHandle.contains(mwh))     {         driver.switchTo().window(popupHandle);         /**/here you can perform operation in pop-up window**         //After finished your operation in pop-up just select the main window again         driver.switchTo().window(mwh);     } } 
like image 102
Santoshsarma Avatar answered Oct 25 '22 23:10

Santoshsarma


You could wait until the operation succeeds e.g., in Python:

from selenium.common.exceptions    import NoSuchWindowException from selenium.webdriver.support.ui import WebDriverWait  def found_window(name):     def predicate(driver):         try: driver.switch_to_window(name)         except NoSuchWindowException:              return False         else:              return True # found window     return predicate  driver.find_element_by_id("id of the button that opens new window").click()         WebDriverWait(driver, timeout=50).until(found_window("new window name")) WebDriverWait(driver, timeout=10).until( # wait until the button is available     lambda x: x.find_element_by_id("id of button present on newly opened window"))\     .click() 
like image 22
jfs Avatar answered Oct 26 '22 00:10

jfs