我正在自动化一个过程,使我应该能够登录到网站并自动下载并重命名文件。
我正在附加代码。我只能下载并重命名文件,其余所有功能都可以正常工作。我认为给xpath有一些问题。
package package1;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;
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.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Test_new {
public static WebDriver setup() throws Exception
{
String downloadFilepath = "E:\\HCA_Automation\\Files";
System.out.println(downloadFilepath);
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
//WebDriver driver = new ChromeDriver(cap);
WebDriver wd;
//String exepath=System.getProperty("user.dir") + "//chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\sh370472\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
return (wd= new ChromeDriver(cap));
}
public static void login(WebDriver wd) throws Throwable
{
wd.manage().window().maximize();
wd.get("https://www.shipper-ml.com");
wd.findElement(By.id("inpUserId")).sendKeys("XXXXXXXX");
wd.findElement(By.id("inpPassword")).sendKeys("XXXXXXXXXXXX");
wd.findElement(By.id("btnLogonLabel")).click();
Thread.sleep(1000);
}
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(),'0123456789'),'06')]/parent::tr/td[7]/a"));
int i = 0;
FileUtils.cleanDirectory(new File("E:\\HCA_Automation\\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(), '0123456789'),'06')]/parent::tr/td[3])["+i+"]")).getText()).substring(0,2));
String report_type = (element.findElement(By.xpath("(//table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(), '0123456789'),'06')]/parent::tr/td[3])["+i+"]")).getText()).substring(0,2);
}
}
public static void tearDown(WebDriver wd) throws Throwable{
wd.quit();
}
public static void main(String[] args) throws Throwable
{
WebDriver wd = setup();
login(wd);
execute(wd);
tearDown(wd);
Thread.sleep(1000);
FileUtils.copyDirectory(new File("C:\\Users\\nea558\\Desktop\\New_Folder\\Files\\TAS\\"), new File( "\\\\zneugo1p17ecn02.bp1.ad.bp.com\\DataTransfer\\ETAP\\DropBoxes\\CATS\\01_Inbox"));
//FileUtils.copyDirectory(new File("C:\\Users\\nea558\\Desktop\\New_Folder\\Files\\PAS\\"), new File("\\\\bp1xeuap2433\\aamon\\scheduler\\Handoffs\\paa\\itd\\process"));
}
}
HTML:
<div id="lc_ctrl258037362">
<table id="ctrl258037362" cellspacing="0" width="845px" class="lc_nf" border="0" cellpadding="0">
<tbody>
<tr>
<td>
<table cellspacing="0" width="100%" class="lcb" border="0" cellpadding="0">
<tbody>
<tr>
<td>
<table cellspacing="1" width="100%" class="ibody" border="0" cellpadding="0">
<colgroup>
<col>
<col>
<col>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<tbody>
<tr class="header">
<td>Report ID</td>
<td>Version No.</td>
<td>Report Type</td>
<td>Contract</td>
<td>Date Created</td>
<td>Status</td>
<td align="center">Excel</td>
<td align="center">XML</td>
</tr>
<tr onmouseover="high(this);" class="even" onmouseout="low(this);" style="">
<td class="cl">19062006</td>
<td class="cl">V1</td>
<td class="cl">PAS ITD/EOD EX1 Allocation Statement</td>
<td class="cl">ETAP</td>
<td class="cl">21/06/2019 08:56</td>
<td class="cl">Published</td>
<td align="center" class="cl">
<a href="/viewReports.do?ctrl=reportListForDownload&action=DownloadReport&param=0" target="_blank">
<img vspace="0" align="absmiddle" border="0" src="images/buttons/excel.gif"></a></td>
<td align="center" class="cl"><a href="/viewReports.do?ctrl=reportListForDownload&action=DownloadXml&param=0" target="_blank"><img
vspace="0" align="absmiddle" border="0" src="images/buttons/document.gif"></a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:0)
将找到下载链接的XPath如下:
//table/descendant-or-self::tr[td[3][text()='PAS ITD/EOD EX1 Allocation Statement']]/td[7]/a
这将找到一个<table>
元素,其中包含一个<tr>
元素,该元素具有一个值为<td>
的{{1}}元素,然后将您路由到PAS ITD/EOD EX1 Allocation Statement
元素在包含下载链接的同一行中。
要将其放入可以调整报告类型值的代码中,可以使用:
<td>
*编辑*
根据您的评论,您变得更复杂了,我现在有:
String reportTypeValue = "PAS ITD/EOD EX1 Allocation Statement"
driver.findElement(By.xpath("//table/descendant-or-self::tr[td[3][text()='" + reportTypeValue + "']]/td[7]/a")
这将找到一个//table/descendant-or-self::tr[td[1][substring(text(), string-length(text()) - string-length('06') + 1)]][td[3][text()='PAS ITD/EOD EX1 Allocation Statement']]/td[7]/a
元素,其中包含一个<table>
元素,该元素具有一个值为<tr>
的{{1}}元素和一个<td>
元素其值为PAS ITD/EOD EX1 Allocation Statement
。然后,它会将您路由到包含下载链接的同一行中的<td>
元素。
XPath 1.0不支持ends -with,因此此块:
06
这可以算出要搜索的文本节点的长度和字符串的长度,并使用子字符串功能来确保最后的x个字符匹配。
要将其放入新的代码块中,您可以用来调整报告类型值和报告ID:
<td>