Reputation: 499
Can someone help me in following issue: I have page with a lot of li elements, as shown below:
<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">
Inside that li list each element has following class:
<span class="original-language-image flag-icon flag-icon-gb"</span>
All li elements have that span class, and only difference is instead above shown -gb, it is different country code.
I have to find first n elements with same gb code (and also those elements has to be one after another), and compare if is same as first element. Rest with different code, I do not need.
Tried with following code but did something wrong (or made mistake by using equals in if statement):
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;
}
}
Thank you in advance
Upvotes: 0
Views: 31
Reputation: 160
Try the following scenario :
1.Select all elements whose class contain the term 'flag-icon-gb'. ( You can restrict the search to one kind of element by replacing the * above. For example to search for all li, you would put //li[...]
2. Iterate over your list to manipulate the first n elements you wish.
3.Do what you want for each element inside the loop (compare, get childrens, click, etc,...)
//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();
}
Edit : it seems that your problem is relative to the comparaison. You are trying to compare Objects (WebElements) and those don't aren't equal.Only their texts are. Try like so instead :
String firstTip = tipsGB.get(0).getAttribute("data-tip-id");
Do the same then inside the loop and then compare
String nextTip = tipsGB.get(p).getAttribute("data-tip-id");
if (nextTip.equals(firstTip)) {
....
}
Upvotes: 1