下载pdf文件

时间:2012-11-17 11:05:10

标签: java selenium

我使用selenium webdriver自动化应用程序,下面是我的代码,工作正常

在此处输入代码:

import java. util.concurrent. TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.JavascriptExecutor;

import com.thoughtworks.selenium.SeleneseTestCase;

public class MonTaxRep1 extends SeleneseTestCase{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get(" URL ");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement un= driver.findElement(By.name("username"));
un.sendKeys("clientremote");
driver.findElement(By.name("password")).sendKeys("12345678");
driver.findElement(By.name("submit")).click();
//  find the element and click on signin
//  driver.findElement(By.id("loginButton")).click();
driver.findElement(By.xpath("//a/span[contains(text(),'Thailand')]")).click();
driver.findElement(By.linkText("Williams Limited Thailand")).click();
driver.findElement(By.xpath("//map[@id='Map']/area[3]")).click();
driver.findElement(By.cssSelector("a > img")).click();
driver.findElement(By.xpath("//img[@onclick=\"showItem('_self')\"]")).click();
new Select(driver.findElement(By.name("pay_year"))).selectByVisibleText("2010");
new Select(driver.findElement(By.name("pay_month"))).selectByVisibleText("January");

driver.findElement(By.linkText("Monthly Tax Report")).click();
driver.findElement(By.name("g_title")).sendKeys("Test1");
driver.findElement(By.xpath("//input[@value='Download']")).click();

当selenium点击下载链接时会出现一个弹出窗口,其中包含两个单选按钮,默认情况下单击单选按钮以打开选项现在我需要将该单选按钮切换为另存为选项并单击确定按钮。 当我单击确定按钮时,pdf文件应保存在某些特定的本地驱动器中。 为此,我使用了以下代码,但它无法正常工作。

//Before  opening pop-up get the main window handle 
String mainWindowHandle=driver.getWindowHandle();
//open the pop-up window(i.e click on element which causes open a new window)
driver.findElement(By.xpath("//input[@value='Download']")).click();

//Below code returns all window handles as set
Set s = driver.getWindowHandles();

Iterator ite = s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}

所以请帮我提供代码, 我怀疑下载的pdf文件是否可以逐行打开和读取,并且可以比较一下这里出现的文本,这可能吗?

2 个答案:

答案 0 :(得分:2)

答案简短:This has been asked many times, please search.

长而正确的答案:截至目前(2012/11),无法通过WebDriver完成。 It's one of the most requested features for the Selenium project.您可以尝试以下其中一项:

  1. 使用HttpURLConnectionApache HttpComponents请求指定的链接。您甚至可以通过这种方式下载文件,尽管通常的做法是断言200 OK响应以确保可以下载文件(因为您在测试应用程序时通常不需要该文件)。
  2. Snatch the file using any Java approach.或由某人制作的this tool与Selenium一起使用。
  3. 使用Robot类只需按向下箭头 Enter 等。但要注意,这只适用于您的特定浏览器和操作系统。它将打破任何其他配置。

答案 1 :(得分:1)

在firefox中,您可以通过将以下代码添加到selenium测试的setUp方法来解决此问题。

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/x-pdf");

如果您要下载pdfs以外的其他类型的文档,则应查找要下载的文档的MIME类型,并将其添加到逗号分隔列表中。

相关问题