如果日期列表中的日期与字符串格式的给定日期匹配,那么它应该从该日期列表中单击该元素,该日期列表仅在获取时转换为字符串。 转换为字符串时,我有以下日期列表:
2017-10-13
2017-09-29
2017-09-22
2017-09-15
2017-09-08
2017-09-01
2017-08-25
2017-08-18
现在,如果输入"2017-Sep-29"
,当解析为另一种格式时,2017-09-29
与列表中的第二个日期相匹配,则应该单击它但是它会抛出错误。
下面是我的带有注释的java代码:
WebDriver driver;
List<WebElement> stageListOfFilters;
String finalSnapshot = "", finalSnapshot1 = "";
@Test
public void getDateAndclick()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date varDate = dateFormat.parse("18-Aug-2017");
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
finalSnapshot1 = dateFormat.format(varDate).toString();
System.out.println("Date: " + finalSnapshot1); // Prints 2017-08-18
driver.findElement(By.xpath(config.getsnapshotFilter())).click();
Thread.sleep(2000);
finalSnap = finalSnapshot1.substring(5, 10);
System.out.println("Date for final Comparison: " + finalSnap); // Prints 08-18 since i want to click on 18th august date
stageListOfFilters = driver.findElements(By.xpath(".//*[@class='QvFrame DS']")); // This collects the list
// of dates which is
// fetched later on in
// for loop
Thread.sleep(2000);
for (int i = 0; i < stageListOfFilters.size(); i++) {
System.out.println(stageListOfFilters.get(i).getText());
Thread.sleep(2000);
if (finalSnap.contains(stageListOfFilters.get(i).getText())) {
driver.findElement(By.xpath(".//*[@class='QvFrame DS']/div/div/div[1]/div[2]/div[" + i + "]"))
.click();// If it matches at that index then it should click on element present at that index
Thread.sleep(2000);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
所以现在它应该点击最后一个元素,因为传递的日期是18th August
,但它仍然点击2017-09-29
。我发现我使用get(i).getText()
if loop
下的for loop
无法获得价值。为什么会这样?
getAttribute(“title”)数据的代码:
stageListOfFilters = driver.findElements(By.xpath(".//*[@id='DS']")); // This collects the list
// of dates which is
// fetched later on in
// for loop
for (WebElement element : stageListOfFilters) {
System.out.println("Dates are: " + element.getAttribute("title"));
}
答案 0 :(得分:0)
尝试以循环方式访问网络元素,如下所示:
stageListOfFilters = driver.findElements(By.xpath(".//*[@class='QvFrame DS']"));
Thread.sleep(2000);
for ( WebElement we: stageListOfFilters ) {
System.out.println(stageListOfFilters.getText());
if (finalSnapshot1.contains(we.getText())) {
we.click();
System.out.println("Data" + we.getText());
Thread.sleep(2000);
}
}
答案 1 :(得分:0)
如果您使用元素正确获取日期值,那么我认为问题可能出现在"if"
条件下。我认为finalSnap
值为"08-18"
,而"stageListOfFilters.get(i).getText()"
则将日期设为"2017-08-18"
。以下&#34;如果&#34; for循环中的条件将始终为false,因为您正在检查"08-18"
是否包含"2017-08-18"
。
if (finalSnap.contains(stageListOfFilters.get(i).getText())) { //this condition check is false
//ur code
}
您应该将"2017-08-18"
与"08-18"
进行比较,而不是相反。
答案 2 :(得分:0)
我无法选择日期列表的正确xpath。我现在解决了它,下面是我的工作代码,它满足了我的期望。对于CSS,您可以查看this链接:
try {
Date varDate = dateFormat.parse("01-Sep-2017");
dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 2017-08-18
finalSnapshot1 = dateFormat.format(varDate).toString();
System.out.println("Date: " + finalSnapshot1); // Prints 2017-08-18
driver.findElement(By.xpath(config.getsnapshotFilter())).click();
Thread.sleep(2000);
stageListOfFilters = driver
.findElements(By.xpath("(//*[@class='QvFrame DS'])[1]/div/div/div[1]/child::div")); // This collects
// the list
// of dates which is
// fetched later on in
// for loop
System.out.println("Size" + stageListOfFilters.size());
Thread.sleep(2000);
for (int i = 1; i < stageListOfFilters.size(); i++) {
if (finalSnapshot1.equals(stageListOfFilters.get(i).getText())) {
System.out.println("Filters list value: " + stageListOfFilters.get(i).getText());
System.out.println("Value to be compared: " + finalSnapshot1);
i = i + 1;
driver.findElement(By.xpath(".//*[@class='QvFrame DS']/div/div/div[1]/div[" + i + "]/div[1]"))
.click();
System.out.println("Value of i: " + i +" and Date is: "+ finalSnapshot1.toString());
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}