我試圖從一個網站保存文件,但無法處理窗口對話框中的窗口 。無法控制對話框點擊保存 File.The代碼應該能夠從url下載文件。如何處理硒中的窗口對話框
我想用下面的代碼:
package new;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BlipsManagement
{
static WebDriver driver;
public static void main(String args[]) throws AWTException
{
//System.setProperty("webdriver.ie.driver","D:\\IEDriverServer.exe");
//driver= new InternetExplorerDriver();
driver= new FirefoxDriver();
try
{
String EIN="xyz";
String pass="abc";
String st_date="01/08/2014";
String end_date="10/09/2014";
String URL="http://myurl.com";
driver.get(URL);
Thread.sleep(2000);
//url opens
driver.findElement(By.name("Logon")).click();
driver.switchTo().activeElement().sendKeys(EIN);
driver.switchTo().activeElement().sendKeys(Keys.TAB);
driver.switchTo().activeElement().sendKeys(pass);
driver.switchTo().activeElement().sendKeys(Keys.ENTER);
Thread.sleep(2000);
//element is located
driver.findElement(By.name("YES")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("locator")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("locator")).click();
Thread.sleep(3000);
//entries are passed
driver.findElement(By.id("PeriodStartDate")).clear();
driver.findElement(By.id("PeriodStartDate")).click();
driver.findElement(By.id("PeriodStartDate")).sendKeys(st_date.toString());
driver.findElement(By.id("PeriodFinishDate")).clear();
driver.findElement(By.id("PeriodFinishDate")).click();
driver.findElement(By.id("PeriodFinishDate")).sendKeys(end_date.toString());
driver.findElement(By.name("locator")).clear();
driver.findElement(By.name("locator")).sendKeys("text");
//checkboxes selected
driver.findElement(By.name("PM1")).click();
driver.findElement(By.name("locator")).click();
driver.findElement(By.cssSelector("input.formButton")).click();
System.out.println("Generated");
Thread.sleep(3000);
//click the word image
driver.findElement(By.linkText("My Reports")).sendKeys(Keys.TAB);
driver.switchTo().activeElement().sendKeys(Keys.ENTER);
//here is where the problem is
//the dialogue box of firefox opens and asks whether to open file or save file.
//initially open file radio button is selected to select save file alt+s is used
//still not working
driver.switchTo().activeElement();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
//robot.keyPress(KeyEvent.VK_ENTER);
//robot.keyRelease(KeyEvent.VK_ENTER);*/
driver.switchTo().activeElement().click();
System.out.println("done!");
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
而不是使用Robot,您可以即時創建FireFoxProfile並將其配置爲自動下載到給定路徑。看到這個線程:http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox – metar 2014-09-11 13:23:02
我也試過,但仍然得到對話框。我是否需要指定可下載文件的鏈接來下載它? – ninja67 2014-09-12 07:01:23