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());
}
Related
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
}
}
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();
Is there a way to Identify and assign WebElement Names to a list of WebElements? For example using the following convention:W
#FindBy(xpath="")
WebElement listFirstObject;
#FindBy(xpath="")
WebElement listSecondObject;
Forgive me if this is an elementary question, but it seems like it would be a pretty common scenario for any tester as many applications have lists of objects with common names. In my case I have a control list with well over 700 objects and it would be nice to be able to write some iterative method to capture and individually create each common WebElement from the list.
** I have made Updates to my question for further clarification** Taking an entire grid of info is completely new to me so please be specific with the answers as I am trying to understand the logic behind it.
So I have elements I am looking for are Grid Data, I have successfully captured the entire Grid, for example
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00")
List<WebElement> someGridData;
If I were to individually capture each new addition to the grid it would look as such:
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__0")
WebElement someGridObj1;
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__1")
WebElement someGridObj2;
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__2")
WebElement someGridObj3;
As you can see each individual grid element ends with "__#" Obviously this is an infinite list and I cannot capture every WebElement individually and assign a WebElement value to use for testing. What I am trying to ask is how do I capture this entire list and then if i need to call an individual WebElement later to test how do I do so? I hope this clarifies and thanks for reading.
Ok, now that have edited your question, it's clear what you want to know. Assuming your grid data is in td elements, you can do the following.
#FindBy(css = "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]")
List<WebElement> allGridData;
By using the ^ (which means starts with) you collect all the td elements that have an id starting with ctl00_SomeGridData_ucControlList_trgControlList_ctl00__.
Yes you can do this like below:
List<WebElement> mylist = driver.findElements(By.xpath("ur xpath"));
System.out.println("Size of the list is : " +mylist.size());
This will give you how many webelements are inside your mylist.
Now apply for loop to print the value of the mylist on console
and also in the for loop you can assign them a value like this
for(int i = 0;i<mylist.size();i++){
System.out.println("index of webelements in increasing order is : " + i + mylist.get(i).getText());
// this will assign an index of 0 to max for the weblements in the
list like index 0 = for 1 webelement ,index 1 = for 2nd webelement ,
index 2 = for 3rd webelement and so on
}
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).
I'm new to Jsoup, but this appears to be a great tool. I'm trying to extract the robots metatag.
I have the following code:
Document doc = Jsoup.parse(htmlContent);
Elements metatags = doc.select("meta");
Element robots = metatags.attr("name", "robots"); // is getting the first element of the list
The last line is wrong.
I want to know if is necessary to run the list of elements to find the element that matches the attribute or there a way that extracts the element that matches the attribute from the Elements list.
Edit 1: I solved this changing to doc.select("meta[name=robots]").
Edit 2: In another words: I want to know how to get all elements in a Elements list that matches some atribute requisite.
Edit 3: I was precipitated doing this question because I had not seen the main documentation yet. Sorry.
It's possible to set the attribute and value you want to retrieve in the select() method to do a better filtering.
Change the select to: doc.select("meta[name=robots]"); and it will get all elements that has the meta tag and it have the name attribute equals robots.
Have you read the JSoup documentation? Here it is from the method you are using:
attr
public Elements attr(String attributeKey,
String attributeValue)
Set an attribute on all matched elements.
Parameters:
attributeKey - attribute key
attributeValue - attribute value
Returns:
this
It returns this. Which means it will return an Elements object. This can't be assigned to an Element object.
I also think you want to use Document.getElementsByTag(String), instead of select.