How to handle duplicate object in selenium? - selenium

I want to send values for particular field
I have found Password locaters value is same on this website.
How to handle same objects which have same id,name,tagname etc.?

You can find them like that :
First one :
driver.findElement(By.xpath("//form[#id='loginForm']/div/div/input[#id='password']")
Second one :
driver.findElement(By.xpath("//form[#id='registration-form']/div/div/input[#id='password']")
With that solution, you'll be sure to find the right element even if an other id=password is added before the one you are looking for.

You can use List<WebElement> to create a list of elements with "same id,name,tagname" and perform indexed operations as :
List <WebElement> elements = driver.<your locator strategy>;
for (int i = 0; i< elements.size(); i++) {
elements.get(0).<perform your action>;
}

Related

How to implement List FindALL webelement

<i>
#FindAll(#FindBy(xpath = ".//input[contains(#name,'adv_xfer_fields')
and contains(#name,'::amounts')]"))
List <WebElement> amounts;
</i>
I have dynamic web-table in the input field, Ideally I need to pass value to this. But I'm not sure how to implement this?
public List<WebElement> getAllAmounts() {
return amounts;
}
Please help
As per your question, I don't see any error in your tried out code but definately we can be a bit structured more precise as follows :
#FindAll({#FindBy(xpath = ".//input[contains(#name,'adv_xfer_fields')]"),
#FindBy(xpath = ".//input[contains(#name,'::amounts')]")})
List <WebElement> amounts;
As per the documentation, FindAll is used to mark a field on a Page Object to indicate that lookup should use a series of #FindBy tags. It will then search for all elements that match any of the FindBy criteria though elements are not guaranteed to be in document order.

how to write xpath for this below situation

I have the elements, highlighted in image :
The xpath for the list is //div[#class='list-group formsFilteredList']//a[#class='list-group-item'], but I need the text of that a tag. The text of that are changes dynamically. So I cannot use text contains method
Try this:
//div[#class='list-group formsFilteredList']//a[#class='list-group-item']/text()
You could find the element by class name instead of xpath but there are several elements with the same class name so what you could do is get the elements into a list and work with them from there:
List<WebElement> lstElements = driver.findElements(By.className("list-group-item"));
Then if you know where in the list the element is that you want to use, you can specify the index. For example if you want to click the second item in the list you'd do this:
lstElements.get(1).click();
If you want to do something with each of the items in turn then iterate through them in a for loop:
for (int i = 0; i < lstElements.size(); i++ ) {
System.out.println("List item text: " + lstElements.get(i).getText());
}

how to print all sugession came in auto suggetions?

i Just started with selenium so i want to know how to handle below thing ...
when type some thing on google it gives us suggestions i want print them on console .
i have tried this
driver.findElement(By.id("lst-ib")).sendKeys("cognizant i");
List<WebElement> lst=driver.findElements(By.tagName("li"));
int ii = lst.size();
System.out.println(lst.get(3).getText());
System.out.println(lst);
for(int i=0;i<lst.size();i++){
System.out.println("hi" + lst.get(i).getText());
}
But not printing any thing on console. plz Guide me where i went wrong .
Following code waits for the suggestions to appear after you have entered some value in google search bar and then prints them one by one in console:
driver.findElement(By.name("q")).sendKeys("Cognizant i");
List <WebElement> allItems = driver.findElements(By.xpath("//*[#id='sbtc']/div[2]/div[2]/div[1]/div/ul//li/div[contains(#id,'sbse')]"));
while(allItems.size() <1)
{}
System.out.println("Total no of elements :" + allItems.size() );
for(int i=0; i< allItems.size() ;i++){
value1= allItems.get(i).getText();
System.out.println(value1);
}
Your code actually runs, and does output the suggestions - however you are grabbing all 'li' elements on the page which results in a large number of empty or irrelevant elements being present in your List lst.
You can see this if you open the Console in your browser on the google home page and search for li tags which is effectively what your code is doing.
Make your code more specific to the list items you want to collect - which reside inside a tag with the role "listbox".
The following will output the suggestions to the console:
driver.findElement(By.id("lst-ib")).sendKeys("cognizant i");
WebElement suggestionList = driver.findElement(By.cssSelector("[role ='listbox']"));
List<WebElement> suggestions = suggestionList.findElements(By.tagName("li"));
for(WebElement suggestion : suggestions){
System.out.println(suggestion.getText());
}

How to select the checkbox in the list of checkboxes ONE-BY-ONE in selenium?

I need to select all the checkboxes from here one-by-one after every 3 second. I tried couple of xpaths with list,none of them have worked
Tried xpaths:
//div/div[#class='filters-list sdCheckbox ']
Using input and type. But none of them worked. Can you please help me out?
Reference website: https://www.snapdeal.com/products/storage-devices?sort=plrty
->Capacity at the left hand corner
By.xpath("//a[#class='filter-name']") this one listed out all the filters of page.
The xPath "//div[#data-name='Capacity_s']/div[#class='filters-list sdCheckbox ']/input" will fetch you the list of all input elements that you need to check.
There is a container DIV that holds all the filters of a certain type, e.g. Brand, Capacity, etc. The one for Brand is shown below.
<div class="filter-inner " data-name="Brand">
Under that container, all the LABEL tags are what you need to click to check the boxes. So, we can craft a CSS selector using the grouping as a filter to reach only the checkboxes we want.
"div[data-name='Brand'] label"
Since this is something I assume you will reuse, I would write this as a function.
public static void CheckFilters(String filterGroup)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> filters = driver.findElements(By.cssSelector("div[data-name='" + filterGroup + "'] label"));
// System.out.println(filters.size()); // for debugging
for (int i = 0; i < filters.size(); i++)
{
filters.get(i).click();
// wait for the two overlays to disappear
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.searcharea-overlay")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.filterLoader.hidden")));
// reload the element list after the refresh so you don't get StaleElementExceptions
filters = driver.findElements(By.cssSelector("div[data-name='" + filterGroup + "'] label"));
}
}
and you would call it like
driver.get("https://www.snapdeal.com/products/storage-devices?sort=plrty");
CheckFilters("Brand");

Selenium using findElements

How can I automate to click the links enumerated by an item list.
*A
*B
*C
*D
*E
xpath of "*A" is html/body/ul/li[1]/a
xpath of "*B" is html/body/ul/li[2]/a
...
Is it possible to click all the items using findElements(By.xpath)?
You can use here findElement method here you can use option to find element it will be more helpful for you. If you want example code than i can provide you feel free to ask me
First answer is yes but! in case that after click you will be not redirected to other page.
you need to create collection that will contain all the li.
IList&ltIWebElement&gt liCollection = driver.FindElement(By.Xpath("html/body/ul")).FindElements(By.TagName("li");
you need to loop them one by one and invoke the click
for(int i = 0 ; i < liCollection.Count ; i++)
{
liCollection[i].FindElement(By.TagName("a")).Click;
//Thread.Sleep(2000);
liCollection = driver.FindElement(By.Xpath("html/body/ul")).FindElements(By.TagName("li"));
}
!!!STACKOVERFLOW CODE FORMAT PROBLEM!!!
try this may help you
//count the list
List<WebElement> ButtonNamelist=driver.FindElements(By.Xpath("html/body/ul"));
int listcount=ButtonNamelist.size();
for(int i=1;i<=listcount;i++){
driver.findElement(By.xpath("html/body/ul/li["+i+"]/a")).click();
//you need to navigate back here to click on the other elements use waits to load the element and click again
}