如何让 System.out.println 仅收集带有子 p 标签的链接?
How make System.out.println collect links only with child p tags?
如何使 System.out.println 只显示带有 /p/ 的链接?我正在尝试编写一个程序,通过标签在 Instagram 上显示所有链接。我能够解析所有带有“a”标签的链接。我现在如何 select 链接到 /P/ 可用的照片?
String hashtag = null;
driver.get("https://www.instagram.com/explore/tags/"+hashtag);
String link_include= "/p/";
List<WebElement> all_links = driver.findElements(By.tagName("a"));
if (all_links.contains(link_include)){
//What do I need to write here for the variable to show links only with the /p/ ?can I use the append method? And How ?
}
}
}
System.out.printlu(all_links.gettext());
要创建具有子 <p>
标签的 <a>
标签列表,您可以使用以下任一方法 :
xpath
:
List<WebElement> all_links = driver.findElements(By.xpath("//a[.//p]"));
要使用 Java8 的 stream()
和 getText()
打印 link 文本:
System.out.println(driver.findElements(By.xpath("//a[.//p]")).stream().map(element->element.getText()).collect(Collectors.toList()));
理想情况下,您需要为 visibilityOfAllElementsLocatedBy()
引入 ,您可以使用以下任一项 :
xpath
:
List<WebElement> all_links = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[.//p]")));
要使用 Java8 的 stream()
和 getAttribute("innerHTML")
打印 link 文本:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[.//p]"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
有人可能对这个问题有疑问。如果我的评论上面的代码不起作用,请尝试这样的操作:// a [starts with (@href, '/ p /')]。请记住,您需要添加滚动条才能获得比第一个 div.
更多的链接
如何使 System.out.println 只显示带有 /p/ 的链接?我正在尝试编写一个程序,通过标签在 Instagram 上显示所有链接。我能够解析所有带有“a”标签的链接。我现在如何 select 链接到 /P/ 可用的照片?
String hashtag = null;
driver.get("https://www.instagram.com/explore/tags/"+hashtag);
String link_include= "/p/";
List<WebElement> all_links = driver.findElements(By.tagName("a"));
if (all_links.contains(link_include)){
//What do I need to write here for the variable to show links only with the /p/ ?can I use the append method? And How ?
}
}
}
System.out.printlu(all_links.gettext());
要创建具有子 <p>
标签的 <a>
标签列表,您可以使用以下任一方法
xpath
:List<WebElement> all_links = driver.findElements(By.xpath("//a[.//p]"));
要使用 Java8 的
stream()
和getText()
打印 link 文本:System.out.println(driver.findElements(By.xpath("//a[.//p]")).stream().map(element->element.getText()).collect(Collectors.toList()));
理想情况下,您需要为 visibilityOfAllElementsLocatedBy()
引入
xpath
:List<WebElement> all_links = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[.//p]")));
要使用 Java8 的
stream()
和getAttribute("innerHTML")
打印 link 文本:System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[.//p]"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
有人可能对这个问题有疑问。如果我的评论上面的代码不起作用,请尝试这样的操作:// a [starts with (@href, '/ p /')]。请记住,您需要添加滚动条才能获得比第一个 div.
更多的链接