WebDriver Get element from elements block - java

I've tried to get child element from list of elements and instead of
return child element value for each item in list -> it returns only
first item.
List<WebElement> allAccoElements = driver.findElements(By.xpath("//ul[#id='ListerContainer']//li[#class='lister-item']//div[#class='lister-item-content']"));
// Found 10 items
for (WebElement element: allAccoElements){
System.out.println(element.findElement(By.xpath("//img[#class='image-base']")).getAttribute("id"));
//For loop will print "id" of first element 10 times, why I can't to get access to other Elements in list?
}
Print always return id of first element in list, can anyone suggest me, how I can find child element of each element in list?
Instead, if I use the following code like workaround, all works fine.
List<WebElement> allAccoElements = driver.findElements(By.xpath("//ul[#id='ListerContainer']//li[#class='lister-item']//div[#class='lister-item-content']//img[#class='image-base']"));
// Found 10 items:
for (WebElement element: allAccoElements){
System.out.println(element.getAttribute("id"));
//Print 10 times with different id
}

Thanks to p0deje we've found the answer:
To find corresponding item inside other, we have to workaround it (by adding dot "." before "//" in xpath).

Related

Is it possible to find common xpath for this elements?

xpath of one element is: //div[#class=name-slider-header']//button//img[2]
xpath of another element is: //div[#class=name-slider-header']//button//img[1]
Actually I need to check attribute of element must contains "red" after element gets disabled
after clicking "n" times, so I am using element.getAttribute("src").contains("red");
element2.getAttribute("src").contains("red");
Is it possible to find common xpath for this elements?
Use the following xpath to identify the image elements where src value contains red
//div[#class='name-slider-header']//button//img[contains(#src, 'red')]
code:
imgElements = driver.findElements(By.xpath("//div[#class='name-slider-header']//button//img[contains(#src, 'red')]"));
The common XPath of these 2 elements is //div[#class=name-slider-header']//button//img. So, you can get a list of elements and then iterate over the list extracting the element attribute, as following:
elements = driver.findElements(By.xpath("//div[#class=name-slider-header']//button//img"));
for(WebElement element : elements){
if(element.getAttribute("src").contains("red")){
// do something
}
}

findElement from a previously filtered page returns the same result

So I have this code (I hope this narrows it down enough):
List<WebElement> resultList = driver.findElements(By.className("result"));
for (WebElement item : resultList) {
String rating = item.findElement(By.xpath("//div[starts-with(#class, 'result-rating')]")).getAttribute("class");
(...)
Let me break down what I think I'm doing:
First I'm separating the nodes that interest me into a list of webElements.
Then I'm cycling through item by item, and trying to get the div element under that item whose class starts with result-rating.
The problem is I always get the same value for rating, which is the first one. If I print out the values I get inside my for, they're right. But when I try to filter it further (3rd line), that particular value always comes up the same.
But how, if each item of my list contains a different node? How can they all have all the page code to return the first only?
And most importantly... how to fix what I'm doing to get the respective rating for every record?
Thank you in advance!
if i get you right, you are first trying to get a list of items by classname("result") and then iterating for each item in list to find an element whose class starts with "result-rating" and then get it's class attribute. If that's the case then you may try below.
List<String> resultRatingList = resultList.stream()
.map(element -> element.findElement(By.xpath("//div[starts-with(#class, 'result-rating')]")).getAttribute("class"))
.collect(Collectors.toList());

xpath how to choose all elements matching

Need to getText from all elements how on the picture 1
I don`t know why it takes text only from the first element
This is my Xpath:
//ul[#class='holders-list ng-scope']//li//article//div//div[#class='price-different clearfix']//ul//li//strong
The following method may works,
use driver.findElementsBy method and store the values in a list , the list will contains all the xpaths
eg :
store the web elements into a list and use a for each loop to iterate over it
List<WebElement> valueList = driver.findElements(By.xpath("//ul[#class='holders-list ng-scope']//li//article//div//div[#class='price-different clearfix']//ul//li//strong"));
for(WebElement value:valueList){
System.out.println(value.getText());
}

Why can't I find in Selenium an element by id when searching for multiple elements

I have a web page with one element that has the unique id "SomeID"
When searching for single element with the method "findElement" it finds the element.
WebElement element = webDriver.findElement(By.id(SomeID))
When using the method for searching multiple elements, I'm getting back an array of 0
List<WebElement> elements = webDriver.findElements(By.id(SomeID));
I don't understand why it doesn't find the element with "findElements".
Is it because "id" should be unique per container?
Will "findElements" work on other types of searches (xpath, etc.)?
Thanks
The locator By.id will always return a single element, even if there's more than one. It's probably because the W3C standard specifies that an id has to be unique, but it's not enforced by the browser.
To get all the elements with same id, use By.cssSelector with a condition on the id attribute:
List<WebElement> elements = webDriver.findElements(By.cssSelector("[id='some-id']"));
Note that you are probably facing a timing issue. In this case use a waiter:
List<WebElement> elements = WebDriverWait(driver, 10)
.until(EC.presenceOfAllElementsLocatedBy(By.cssSelector("[id='some-id']")));

How to skip first element in webpage and choose second element using selenium

I have two elements in webpage with same linktext in same table structure. I have to ignore first element and select second element everytime if present.
But problem is selenium selecting first element every time, How can I select second element instead of first element(ignore first element)?
I can't use anything other than Linktext to identify that element using selenium, That's the constraint.
when elements having same link text or having same loactors, there is one collection present in java i.e List
create a list of webelements having such kind of scenario, then by there index you can access the elements.
List<WebElement> list1 = driver.findElements(By.linkText("Services"));
for(int i=0;i<list1.size();i++)
{
System.out.println(i+" "+list1.getText());
//this can be used incase number of elements is more and no time to count there index
}
list1.get(1).click();
Here it is for getting second element via link text where I used collection class to store all same kind of elements.
List<WebElement> li = driver.findElements(By.linkText("Services"));;
li.get(1).click();

Categories