收集< li>硒webdriver中u的元素 [英] collect <li> elements of <u>l in selenium webdriver

查看:108
本文介绍了收集< 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"));

这篇关于收集&lt; li&gt;硒webdriver中u的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆