Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using selenium webdriver + java + eclipse + testng for my automation scripts.

I am trying to get the URL of window which contains an alert box. On clicking download button on a webpage, it opens an alert box in a new window. I want to fetch the URL of this window.

I tried getCurrentURL command for this but i am getting UnhandledAlertException : Modal dialog present. If i dismiss the alert box the window containing is immediately closed so it is not possible to get the URL.

It seems the alert box (modal dialog here) is blocking webdriver in reading the URL of the window. Please suggest me a solution for this.

Thanks

share|improve this question
    
What is the alert box? Is it a windows box? –  niharika_neo Dec 12 '12 at 12:19
    
I don't know the exact solution, but i can suggest you a workaround for this. Hope it helps. get the page source of the page in which download button is present using getPageSource() method and you can find the link attached to download button properties(id or name or xpath). –  HemChe Dec 12 '12 at 12:47
    
After clicking on download button is it opening new popup window? Do you want popup window URL? What is the alert coming? Can't you handle that alert ? –  Santoshsarma Dec 12 '12 at 14:02

2 Answers 2

I'm not sure I understand the question as an alert box does not have any URL !

Anyhow, you can access it this way : Alert alert = webDriver.switchTo().alert(); You can then retrive the text content or interact with it as described in here : http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Alert.html

share|improve this answer
    
The alert box is a file download pop up which contains options for save , open and cancel. This pop up opens in a new browser window and there is a URL in the window which contains some information about the file that is being downloaded. –  megha Dec 13 '12 at 3:21
    
@HemChe I tried getPageSource() but getting the same exception Modal Dialog Present. –  megha Dec 13 '12 at 3:29
    
I am able to handle the alert by using accept() and dismiss() commands but on doing so the alert closes along with the window containing the alert. So the URL is again lost. The URL is present is address bar of the window and not in alert box. –  megha Dec 13 '12 at 3:39
    
@megha : I think you have to play with driver.switchTo().window(); After, the question is to find the correct handle. Either it is the window that opens the alert(), then String winHandleBefore = driver.getWindowHandle(); is the way to go. Otherwise, use driver.getWindowHandles() to try to locate it. Is it better like that ? –  n1r3 Dec 15 '12 at 19:58

I think you need to get the set of windowHandles first for this driver.getWindowHandles();. Then get the required Handle of the newly opened window by iterating them. After that you can switch to the opened window using driver.switchTo().window("pass the handle here");

Now your control comes to new window. Then use like driver.getCurrentUrl();

Hope this could help you. Best Regards :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.