收集< li>硒webdriver中u的元素

收集< li>硒webdriver中u的元素

问题描述:

我的网页上有广告列表.这些广告显示在ID为adList的标签下.我想从那些广告中点击任意随机广告

I have list of ads on my webpage. Those ads are displayed under tag with id adList. From those ads I want to click on any random ad

所以我所做的如下:

List<WebElement> allads = driver.findElements(By.id("adList"));
assertNotNull(allads);
System.out.println(allads.size());
Random random = new Random();
int index = 0;
for (int i = 1; i <= allads.size(); i++) {    
      index = random.nextInt(allads.size());
}
allads.get(index).click();

但是当我运行代码时,没有广告被点击,当我检查allads.size()时,它显示为1,这是错误的,因为它具有以下多个ID

But when I run the code no ad is getting clicked and when i checked allads.size() count it is shown as 1 which is wrong as has multiple ids as following

<ul id="adList" data-pageno="2">
<div id="feedList">
<li id="210846759">
<li id="210847160">
<li id="210845605">
<li id="210841804">
<li id="210846830">
<li id="210846163">
<li class="adbannerli">
<li id="210844135">
<li id="210845467">
<li id="210619597">
<li id="200780114">
<script type="text/javascript">
</ul>

我应该怎么做才能获取所有li元素并点击任意随机广告

What should I do to get all li elements and click on any random ad

您只是尝试查找ul的列表,但实际上您需要更深入地查找所有li元素

You just try to find the list of ul but in fact you need to go more deep and find all li elements

List<WebElement> allads = driver.findElements(By.cssSelector("#adList li"));