Java Webdriver-查找具有相同类的第一个x元素
有人能帮我解决以下问题吗 我有一个包含很多li元素的页面,如下所示:Java Webdriver-查找具有相同类的第一个x元素,java,webdriver,Java,Webdriver,有人能帮我解决以下问题吗 我有一个包含很多li元素的页面,如下所示: <ul class="feed-tips" id="Grid" data-sport="" data-country="" data-league=""> <li class="feed-item vevent tip-list-row" data-sort-bookmaker="12" data-sort-datetime="1525849200" data-sort-match="1" data-sort
<ul class="feed-tips" id="Grid" data-sport="" data-country="" data-league="">
<li class="feed-item vevent tip-list-row" data-sort-bookmaker="12" data-sort-datetime="1525849200" data-sort-match="1" data-sort-odds="1.80" data-sort-rating="3" data-sort-status="1" data-sort-yield="-2.106" data-tip-id="6900510">
<li class="feed-item vevent tip-list-row" data-sort-bookmaker="22" data-sort-datetime="1525852800" data-sort-match="2" data-sort-odds="2.59" data-sort-rating="2" data-sort-status="1" data-sort-yield="-3.082" data-tip-id="6900483">
<li class="feed-item vevent tip-list-row" data-sort-bookmaker="22" data-sort-datetime="1525852800" data-sort-match="3" data-sort-odds="2.21" data-sort-rating="2" data-sort-status="1" data-sort-yield="-4.118" data-tip-id="6899865">
在该li列表中,每个元素都有以下类:
<span class="original-language-image flag-icon flag-icon-gb"</span>
所有li元素都有那个span类,唯一的区别是上面显示的-gb,这是不同的国家代码
我必须找到具有相同gb代码的前n个元素,而且这些元素必须是一个接一个的,并比较是否与第一个元素相同。用不同的代码休息,我不需要
尝试使用以下代码,但在if语句中使用equals时出错或出错:
List<WebElement> tipsGB = driver.findElements(By.xpath("//ul[@class='feed-tips']/li/div[@class='author medium-3 small-12 column padding-reset tip-list-row__author']\n"
+ "//div[@class='original-tip-language-container']/span[contains(@class,'flag-icon-gb')]"));
WebElement firstTip = tipsGB.get(0);
for (int p = 1; p < tipsGB.size(); p++) {
System.out.println(tipsGB.get(p));
WebElement nextTip = tipsGB.get(p);
if (nextTip.equals(firstTip)) {
WebElement tipLink = nextTip.findElement(By.xpath("../../../..\n"
+ "/div[@class='tip medium-9 small-12 column padding-reset dtstart tip-list-row__tip']\n"
+ "/div[@class='tip-match medium-12 column']/div[@class='tip-teams']/a"));
System.out.println("Link to a tip with same language is: " + tipLink.getAttribute("href"));
} else {
System.out.println("No more tips in same language on top of page");
continue;
}
}
提前感谢您尝试以下场景:
1.选择其类包含术语“标志图标gb”的所有元素。通过替换上面的*可以将搜索限制为一种元素。
例如,要搜索所有li,您可以将//li[…]
二,。在列表上迭代以操作所需的前n个元素
3.对循环中的每个元素执行所需操作:比较、获取子元素、单击等等
//Get all flags containing GB
List<WebElement> tipsGB = driver.findElements((By.xpath("//*[contains(@class,\'flag-icon-gb\')]"))
//Iterate over the list and do your stuff
for(int i=0; i<numberofElementsYouWant<;i++){
Webelement currentElement = tipsGB.get(i);
//manipulate your elements here
currentElement.Dostuff();
}
然后在循环内执行相同的操作,然后进行比较
String nextTip = tipsGB.get(p).getAttribute("data-tip-id");
if (nextTip.equals(firstTip)) {
....
}
列表的路径更短,但我已经找到了包含类的所有元素。我感到困惑的是,为什么当我选择索引为0的第一个元素,并试图将其与下一个元素进行比较时,它不起作用。尝试使用if。零元素。等于。下一步通过迭代我认为错误与对象的比较有关,而不是文本,我编辑了我的答案。
String nextTip = tipsGB.get(p).getAttribute("data-tip-id");
if (nextTip.equals(firstTip)) {
....
}