简体   繁体   中英

How to handle windows pop ups using selenium 2.0 (webdriver)?

enter code here Brief : I am currently using selenium 2.0 to automate the testing of an UI.Everything was fine till yesterday till i came across pop ups.I am having to handle windows popups.

The problem : I am navigating to page using the following code =>

driver.get("http://xxx.xx.x.xxx:zzzz/yyyy/"); 
        driver.findElement(By.name("username")).sendKeys("username");
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.className("rowClass")).submit();
        driver.findElement(By.name("uploadfile")).click();  //this is the browse button

Now the problem arises when i click the browse button.It opens up another windows browse file window.What i need to do is to select a file by navigating to the given path and then select a particular file and i am not able to do the same currently.What could be the suggestion.Some one said it was not possible to do the same using selenium 2.0 and to use autoit instead.if someone knows how to do it please let me know else please suggest a better way to get it done.Thanks and regards.

PS :

//this is mentioned as the solution in http://seleniumhq.org/docs/03_webdriver.htmlbut did not work in my case

Alert alert = driver.switchTo().alert(); 

This is more of a "How to upload a file in WebDriver?" question which has been asked many times :).

Selenium 2 (WebDriver) Java example:

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");

The idea is to directly send the absolute path of the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element (or the Browse button as you call it).

Also, the Alert interface is just for popup JavaScript dialogs - alert , confirm , prompt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM