我试图点击给定网页的循环。一旦点击,我需要再次抓取链接。我被困在必须从堆栈中弹出链接的地方。我如何继续收集其他链接并删除重复项?
public static void path(String url){
WebDriver driver = new FirefoxDriver();
driver.get(url);
Deque<String> stack = new ArrayDeque<String>();
boolean goal = true;
while(!goal){
for(WebElement links: driver.findElements(By.tagName("a"))){
System.out.println(links.getAttribute("href"));
stack.push(links.getAttribute("href"));
}
for(int i = 0; i < stack.size();i++){
if(stack.)// remove duplicates ????????
driver.get(stack.pop());
i++;
}
}
}
HTML
<div id="menunav">
<ul>
<li><a href="index.php"><span>Home</span></a></li>
<li><a href="chinese_menu.php"><span>Menu</span></a></li>
<li><a href="chinese_food_catering.php"><span>Catering</span></a></li>
<li><a href="restaurant_events.php"><span>Events</span></a></li>
<li><a href="gallery.php"><span>Gallery</span></a></li>
<li><a href="contact.php"><span>Contact</span></a></li>
</ul>
</div>
答案 0 :(得分:1)
List<WebElement> links = driver.findelements(By.cssSelector("a[href]"));
通过这种方式,您将获得具有 href 属性的所有链接。
您可以遍历列表并对元素进行操作:
for(int i =0; i< links.size(); i++)
{
links.get(i).click();
// and|or get text:
// links.get(i).getText();
}
使它适合你(在问题描述时)你应该用while
和布尔条件标志包装它:
bool condition=true;
while(condition)
{
links = driver.findelements(By.cssSelector("a[href]"));
for(int i =0; i< links.size(); i++)
{
links.get(i).click();
// and|or get text:
// links.get(i).getText();
if(..analysis for condition goes here...) {
condition=false;
}
}
}