使用Selenium Webdriver从网站下载文件时出现问题

时间:2019-06-17 15:07:49

标签: java selenium

我必须从网站上下载2个文件,然后根据报告类型重命名文件,然后将其移至特定文件夹。

enter image description here

我尝试使用xpath下载文件,但无法这样做。

//*@id="ctrl1805929160"]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[10]/td[1]

public static void execute(WebDriver wd) throws Throwable{
wd.get("https://www.shipper-ml.com/viewReports.do");  
Thread.sleep(2000);
List<WebElement> list= wd.findElements(By.xpath("//table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),06)]/parent::tr/td[10]/a"));
int i = 0;
FileUtils.cleanDirectory(new File("C:\\Users\\NEA558\\Desktop\\New_Folder\\Files"));
for (WebElement element:list)
{
    i++;
    element.click();
    Thread.sleep(1000);
    System.out.println((element.findElement(By.xpath("(//table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),06)]/parent::tr/td[10])["+i+"]")).getText()).substring(0,3));
    String report_type = (element.findElement(By.xpath("(//table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),06)]/parent::tr/td[10])["+i+"]")).getText()).substring(0,3);

} 
}

表格来源:https://www.shipper-ml.com

enter image description here

enter image description here

我在执行代码时收到错误消息。

Element info: {Using=xpath, value=//table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(),'0123456789'),'06')]/parent::tr/td[1]/a}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:513)
at org.openqa.selenium.By$ByXPath.findElements(By.java:356)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
at package1.Test_new.execute(Test_new.java:80)
at package1.Test_new.main(Test_new.java:105)

我相信我在给xpath时犯了一些错误。有人可以帮我吗?

0 个答案:

没有答案