我正在尝试使在线游戏自动化,但被困了。
我正在尝试做的事情:我正在尝试分析我的报告。第一步涉及获取父行。第二步是根据是否读取报告以及报告是否为今天的日期来过滤父行。
this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
&& reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
.map(reportRow -> {
String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
String bounty = reportRow.findElement(this.bountyElement).getText();
System.out.println("Village: " + villageName + " Bounty: " + bounty);
return new String[]{bounty, villageName};
})
.forEach(entry -> System.out.println("Entry:" + entry[0] + ", " + entry[1])
这里是我要解析的HTML。
<tr>
<td class="sel">
<input class="check" name="n1" type="checkbox" value="14180678">
</td>
<td class="sub newMessage">
<a href="berichte.php?id=14180678&t=1&s=0&page=1&toggleState=0">
<img alt="unread" class="messageStatus messageStatusUnread" src="img/x.gif">
</a>
<img alt="Won as attacker with losses." class="iReport iReport2 " src="img/x.gif">
<a class="reportInfoIcon" href="build.php?id=39&tt=2&bid=14180678"><img alt="16/100" class="reportInfo carry half"
src="img/x.gif"></a>
<div class="">
<a href="berichte.php?id=14180678%7C5fa6b3d7&t=1&s=1">mwldjt raids Natars 36|64</a>
</div>
<div class="clear"></div>
</td>
<td class="dat">
24/04/20, 04:08 am
</td>
</tr>
我正在尝试在LinkedHashmap中捕获赏金和村庄名称。我现在用打印方法替换了foreach块中的'put'。
问题:
我需要执行两项操作-从位于父行内的两个元素中获取数据。我一直在尝试了解如何执行此操作,但是现在我认为我需要帮助。感谢您抽出宝贵的时间。
在forEach
上具有put
操作的LinkedHashMap
可以转换为使用
.collect(Collectors.toMap(<key-fn>, <value-fn>, <merge-fn>, LinkedHashMap::new)
因此您可以将分片的代码重构为
LinkedHashMap<String, String> output = this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
&& reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
.map(reportRow -> {
String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
String bounty = reportRow.findElement(this.bountyElement).getText();
System.out.println("Village: " + villageName + " Bounty: " + bounty);
return new String[]{bounty, villageName};
})
.collect(Collectors.toMap(e -> e[0], e -> e[1], (a,b) -> b, LinkedHashMap::new);
当然,可以通过诸如以下形式提取获取村庄名称和赏金的操作,并将其直接放置在toMap
收集器中:
LinkedHashMap<String, String> output = this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> isValidReportElement(reportElement))
.collect(Collectors.toMap(reportElement -> getVillageName(reportElement),
reportElement -> getBounty(reportElement), (a,b) -> b, LinkedHashMap::new);