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
}
}
Related
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());
}
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();
I am trying to identify a xpath of a textfield where the class name is "DeleteBackwardTextField" and Origin class is "UITextField". There are no label, name and text it. No other information to identify it.
There are 2 textfield with same above properties.
So my following xpath identifies two text fields.
By.xpath("//textfield[#class='DeleteBackwardTextField']")
How can I identify the xpath of 1 particular field. I assume index can be used. Can anybody provide a syntax for indexing.
Appreciate your help.
You can go with XPATH or CSS. The trick is to have a list of WebElements identified and then you can iterate over it.
List<WebElement> myList = driver.findElements(by.cssSelector(...); //or XPATH;
for (WebElement txtField : myList) {
// do what you need here, for each element at a time
}
Use this xpath.
(//textfield[#class='DeleteBackwardTextField'])[index]
EX: (//textfield[#class='DeleteBackwardTextField'])[1] -> first element when you have many elements matching //textfield[#class='DeleteBackwardTextField']
Note that this xpth - //textfield[#class='DeleteBackwardTextField'][2] has different meaning - it means second child of its parent.
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.