http://www.toysrus.com/family/index.jsp?categoryId=2535588&sr=1&origkw=watches
在上面的网页中有很多产品可供选择,我们需要使用java selenium随机点击产品链接。请帮帮我!
答案 0 :(得分:0)
List<WebElement> links = driver.findElements(By.cssSelector('a.prodtitle'));
links.get(new Random().nextInt(links.size())).click();
试试这个
答案 1 :(得分:0)
The answer already posted will only click one link at random. If you eventually want to click every link randomly, then you will need to keep some sort of record of what links you have visited already. You will also need to go back after visiting a page.
List<WebElement> links = driver.findElements(By.cssSelector('a.prodtitle'));
List<WebElement> visited = new List<WebElement>();
WebElement random = links.get(new Random().nextInt(links.size()));
if( !visited.contains(random))
{
random.click();
visited.add(random);
driver.navigate().back();
}