我无法编写脚本。如何使用selenium webdriver在chorme浏览器中下载excel文件

时间:2017-04-21 08:53:46

标签: selenium selenium-chromedriver

这是我的网站网址 - 117.247.65.9/vms_test。登录用户名-tane,密码-12345。 1)登录系统,2)点击进入数据管理模块,3)点击进入下载数据录入模板。我无法编写脚本供下载。这是我的代码 -

public class DownloadUpload {
    public static void main(String[] args) throws InterruptedException, AWTException {


    System.setProperty("webdriver.chrome.driver","C:\\Users\\Arijit Mohanty\\Desktop\\chromedriver.exe");
    WebDriver fd = new ChromeDriver();

    fd.get("http://117.247.65.9/vms_test");
    fd.manage().window().maximize();
    fd.findElement(By.id("j_username")).sendKeys("thane");
    fd.findElement(By.id("j_password")).sendKeys("12345");
    fd.findElement(By.id("log")).click();
    Thread.sleep(5000);

    WebElement e1 = fd.findElement(By.xpath("//a[contains(.,'Data Management')]"));
    Actions act = new Actions(fd);
    act.moveToElement(e1).build().perform();
    Thread.sleep(5000);


    fd.findElement(By.xpath("//a[contains=(.,'downloadDataEntry')]")).click();



}

}

执行脚本后会出现错误消息。我无法在Google Chrome浏览器中运行该脚本。请帮我。如何编写下载数据输入excel的脚本。

2 个答案:

答案 0 :(得分:0)

更新并检查下面的代码,它工作正常。而且,如果你避免使用硬等待(thread.sleep),使用显式或流畅的等待会更好。

System.setProperty("webdriver.chrome.driver","C:\\Users\\Arijit Mohanty\\Desktop\\chromedriver.exe");
WebDriver fd = new ChromeDriver();

fd.get("http://117.247.65.9/vms_test");
fd.manage().window().maximize();
fd.findElement(By.id("j_username")).sendKeys("thane");
fd.findElement(By.id("j_password")).sendKeys("12345");
fd.findElement(By.id("log")).click();
Thread.sleep(5000);
WebElement e1 = fd.findElement(By.xpath(".//*[@id='menu']/ul/li[2]/a"));
Actions act = new Actions(fd);
act.moveToElement(e1).clickAndHold().build().perform();
fd.findElement(By.xpath(".//*@id='menu']/ul/li[2]/ul/li[1]/a")).click();
Thread.sleep(2000);

如果适合您,请更新我。

答案 1 :(得分:0)

对代码的更正:

  1. 不要使用硬等待(即Thread.sleep())

  2. 您为下载数据条目提供的Xpath错误。

  3. 以下是代码:

    import java.awt.AWTException;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class DownloadUpload { public static void main(String[] args) throws InterruptedException, AWTException {
    
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Arijit Mohanty\\Desktop\\chromedriver.exe");
        WebDriver fd = new ChromeDriver();
    
        WebDriverWait wait = new WebDriverWait(fd,15);
    
        fd.get("http://117.247.65.9/vms_test");
        fd.manage().window().maximize();
        fd.findElement(By.id("j_username")).sendKeys("thane");
        fd.findElement(By.id("j_password")).sendKeys("12345");
        fd.findElement(By.id("log")).click();
    
        wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Data Management")));
        WebElement e1 = fd.findElement(By.xpath("//a[contains(.,'Data Management')]"));
    
        Actions act = new Actions(fd);
        act.moveToElement(e1).build().perform();
    
    
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'Download Data Entry Template')]")));
        fd.findElement(By.xpath("//a[contains(.,'Download Data Entry Template')]")).click();
    }
    }
    

    尝试一下,让我知道它是否适合你。