您的位置:首页 > 运维架构

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

2012-09-13 13:16 801 查看
http://stackoverflow.com/questions/9188343/how-can-selenium-web-driver-get-to-know-when-the-new-window-has-opened-and-then

3
down vote
favorite
1

i am facing below issue in automating web application using selenium web driver.

Webpage has a button which when clicked opens new window.when i use the below 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 above issue i used wait between button click and switch 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();

This solved the issue. But i do not want to use "Thread.Sleep(50000);" statement becauseif 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???

testing

selenium
window
webdriver
share|improve
this question
asked
Feb 8 at 5:37




Ozone

2713

75% accept rate
feedback

2 Answers

activeoldestvotes

up vote
2
down vote
accepted
You need to switch the control to pop-up window before doing any operations in it.

In this way 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 you handle 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);
}
}


share|improve this
answer
edited
Feb 9 at 14:34





Jav_Rock

5,64531650

answered
Feb 8 at 14:51




Santoshsarma

764

Thanks for the reply.It is working. – Ozone
Feb
14 at 4:45
feedback





up vote
0
down vote
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()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐