如何使用selenium 2.0(webdriver)处理Windows弹出窗口?

时间:2012-05-24 06:03:56

标签: java selenium webdriver

enter code here简介:我目前正在使用selenium 2.0来自动测试UI.Everything直到昨天才流行起来。我不得不处理Windows弹出窗口。

问题: 我使用以下代码导航到页面=>

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

现在当我点击浏览按钮时出现问题。它打开另一个Windows浏览文件窗口。我需要做的是通过导航到给定路径选择一个文件,然后选择一个特定的文件,我不能目前也做同样的事情。可能是什么建议。有人说使用selenium 2.0不能做同样的事情而是使用autoit。如果有人知道该怎么做请告诉我其他请建议一个更好的方法完成它。感谢和问候。

PS:

//提到这是因为http://seleniumhq.org/docs/03_webdriver.htmlbut中的解决方案在我的情况下不起作用

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

1 个答案:

答案 0 :(得分:0)

这更像是“如何在WebDriver中上传文件?”问题many times:)。

Selenium 2(WebDriver)Java示例:

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

我们的想法是直接将文件的绝对路径发送到您通常单击的元素以获取模态窗口 - 即<input type='file' />元素(或Browse按钮它)。

此外,Alert界面仅适用于弹出式JavaScript对话框 - alertconfirmprompt

相关问题