WebDriver PageFactory Find Elements List - java

I have multiple elements on a page and I would like to initialize them using PageFactory.
I have tried using following
#FindBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
but this returns only one element.
now, if I use the traditional way for finding elements
List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(#class,'x-grid-tree-node-leaf')]"));
this returns 4 elements
any pointers what could be the issue?

#FindBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
this works. there was bug in my code.

Use FindAll annotation to get series of #FindBy tags and search for all elements that match any of the FindBy criteria.
#FindAll(#FindBy(how = How.XPATH, using = "//*[contains(#class,'x-grid-tree-node-leaf')]"))
List<WebElement> allElements;

Instead of using #FindBy annotation, use #FindAllBy annotation.Try this!
#FindAllBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
Here's the link for FindAllBy java class.

Have you tried running your xpath in Chrome Developer tool or in Firebug?
List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(#class,'x-grid-tree-node-leaf')]"));
should work.

Related

How to find classname starting with a particular text and ending with a particular text through Selenium and Java

Below is the element and I need to find the class names matching with the first css- and ending with widget, How do i find it in selenium (java)?
Tried with the below code, but it didnt work
List<WebElement> list1 = driver.findElements(By.xpath("//body[starts-with(#class, 'css-')]"));
List<WebElement> list2 = driver.findElements(By.xpath("//body[contains(#class, 'css-')]"));
List<WebElement> list3 = driver.findElements(By.xpath("//body[ends-with(#class, 'widget')]"));
I use CSS selectors, to find any element of any type:
driver.findElements(By.CssSelector("[class^='css-'][class$='widget']"))
Where we find any element, which has a class attribute using [], that begins with ^= and ends with $=
To find a div:
driver.findElements(By.CssSelector("div[class^='css-'][class$='widget']"))
#elworthy's answer was in the right direction about the CssSelector which you can use and as per your code trials the following should work:
List<WebElement> list3 = driver.findElements(By.CssSelector("//body[class^='css-'][class$='widget']"));
However if you want to construct a xpath it is worth to mention Selenium supports xpath v1.0 and ends-with() is part of xpath v2.0. So the equivalent xpath will be:
List<WebElement> list1 = driver.findElements(By.xpath("//body[starts-with(#class, 'css-') and contains(#class, 'widget')]"));

How do I write a xpath with conditions to get a list of desired webelements for my requirement

I want to get a list of webelements for which I wrote a common xpath to retrieve all the webelements. Problem is the list contains a webelement, which I don't require in my code.
List<WebElement> tbodyTD2 = InitDriver.driver.findElements(By.xpath("//tr[#id='addBrandTbl']/td/table/tbody/tr/td"));
Which returns me 9 webelements. out of which 8 are required.
The xpath for the webelement which i don't want in my list is :
.//*[#id='addBrandTbl']/td/table/tbody/tr[3]/td
Is there any way to write my xpath so that I can get a list excluding the above element?
You can write an XPath predicate to exclude element in certain position index :
By.xpath("//tr[#id='addBrandTbl']/td/table/tbody/tr[position() != 3]/td")
You can just remove this element from the list
tbodyTD2.remove(3);
Try this one
List<WebElement> webElements = driver.findElements(By.xpath("//tr[#id='addBrandTbl']/td/table/tbody/tr/td"));
List requiredElementsList=new ArrayList();
for (WebElement element : webElements) {
if (!element.equals("unwantedWlement")) {
requiredElementsList.add(element);
}
}
Got
xpath: //tr[#id='addBrandTbl']/td/table/tbody/tr/td[not(#colspan)]

Selenium cssSelector works in IDE but not in Webdriver

I have a web system I am automating using Java/Selenium Webdriver. I have an item I am trying to get access to. It has a compound class name. I have tried all the solutions I have been able to find here and so far none of them work.
The most offered solution looks like this:
By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());
When I check the size of the elements array it is always zero.
What I am finding however is that when I put the selector string in to the Selenium IDE (2.9.1) and use the "Find" button it identifies the correct web element without any problem at all.
I am at a loss for why it works in the IDE but not in my code.
Try selecting the element using its XPath? In the past when I ran into issues trying to select something using cssSelector, I often had success when I tried its XPath instead.
Give some wait time before the selector you are taking.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());
or try to find the elements with the help of Xpath or id.
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
Hope it will help you

Selenium WebDriver can't find element by #FindBy annotation

I want to find this element
driver.findElement(By.className("name")).findElement(By.tagName("a"));
is there way to use #FindBy annotation?
This is possible. Selenium supports both #FindBys and #FindAll which adds finer tuning for page objects.
For chained element look-up, (which is what you're doing, a look-up inside a look-up), you'll want #FindBys.
#FindBys({#FindBy(className = "name"), #FindBy(tagName= "a")})
private WebElement element;
Further reading on FindBys and FindAll.
Since you are looking for an "a" tag inside an element with the "name" class, try combining them via XPath or CSS selectors:
#FindBy(xpath = "//*[#class = 'name']/a")
private WebElement nameLink;
or
#FindBy(css = ".name > a")
private WebElement nameLink;
I several test FindBys annotation in selenium but sometime when use
TageName and Name it's not correctly work!!.
ofter Find List<WebElement> use driver.findElement(By.name("....")) to do filter.

Webdriver search within element

In Telerik it’s possible to search elements within an element that was already found. E.g. I found an ul, that has some elements li. After that I can invoke find() directly from the element.
Is there such possibility using the WebDriver Java?
In WebDriver the usual way of finding an element on the page is;
WebElement element = driver.findElement(By.xpath("xpath query here"));
The findElement method is provided by SearchContext inferface, which WebElement also extends. This means you can call findElement on any element found by a previous search;
WebElement child = element.findElement(By.xpath("another xpath query"));
Actually, you can use
List<WebElement> = driver.findElements(By.cssSelector(".ul li"));
Your list contains all li elements who are in the ul.

Categories

Resources