How to get the total search result counts using selenium - selenium

I want to get the result count when i do a search in amazon. Following is the scenario
Open a new browser instance
Navigate to the amazon.in
Type the search query in the text box
Click on the search button
Validate the web element displaying the number of search results against our expected value
public void searchTestOne(){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.amazon.in");
driver.manage().window().maximize();
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Books");
driver.findElement(By.className("nav-submit-input")).click();
int result = driver.findElements(By.xpath(".//*[#id='atfResults']/ul[#id='s-results-list-atf']/li")).size();
System.out.println(result);
driver.close();
driver.quit();
}
From above code it displays only count of first page result like "16" where as there so many pages and total result is 2,000+.
Can anyone please suggest on this.

Webelment used to get total search result count is not correct. Correct element/tag is "h2" with id "s-result-count"
Below line should give you number of search result count:
String result = driver.findElement(By.id("s-result-count")).getText().split(" ")[2];

It seems like you are trying to fetch the number of books by counting the number of books enlisted. By this, you would have to navigate to each page, and increase your count accordingly. Rather, you can get the total number of results, alternatively, if you look at the top-left of the page as shown in the image below:
So, you can get the total count of results from this code as #Surya suggested:
String result = driver.findElement(By.id("s-result-count")).getText().split(" ")[2];
System.out.println(result);

Try this:
result = driver.findElements(By.xpath(".//*[#id='atfResults']/ul[#id='s-results-list-atf']/li")).count();

Related

How to print this ajax text in selenium?

I am trying to automate Snapdeal website.
Steps followed:
SnapdealHome->Hover on Computer&Gaming->Storage->Select 1TB and 16gb both.
Then I want to print what all does the Capacity filter contains.
I tried this xpath but it doesn't show anything.
Is this an ajax code?
I want to print what all does the capacity.getText() contains.
These are the xpaths I tried:
$x("//div[#class='navFiltersPill'][contains(text(),'Capacity:')]") and $x("//div[#class='navFiltersPill']").
Output should be: 1TB and 16GB both. Is it possible?
Check the below attached figure
Please try to use findElements with below xpath and then retrieve the text by looping all matching elements one by one. This will specifically list out all the filtered conditions under Capacity filter section. Change the below data-name value in the xpath according to your requirement.
//div[#data-name='Capacity_s'][contains(#class,'filter-inner filter-inner-height')]/div[contains(#class,'active-filter')]/label/a
Sample code: [Make sure to add wait before hitting this line]
List<WebElement> ele=d.findElements(By.xpath("//div[#data-name='Capacity_s'][contains(#class,'filter-inner filter-inner-height')]/div[contains(#class,'active-filter')]/label/a"));
for (WebElement e:ele){
System.out.println(e.getText());
}
You will have to loop through all the checked INPUTs and print their value. For example,
for (WebElement e : driver.findElements(By.cssSelector("input[checked]")))
{
System.out.println(e.getAttribute("value"));
}
will print
1%20TB
16GB
The %20 is a space.

Selenium: Cannot print values from different rows of a webtable

I am trying to print the values of the City column for different rows from the below table shown in the image.
I used the below code. My code prints "Dubai" 4 times instead of printing different cities. Can someone help me in fixing this?
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-table/");
WebElement body = driver.findElement(By.xpath("//*[#id='content']/table/tbody"));
List<WebElement> rows = body.findElements(By.tagName("tr"));
System.out.println(rows.get(0).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(1).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(2).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(3).findElement(By.xpath("//td[2]")).getText());
Table Image
Actually your code is correct, only problem with your xpath using for getting city element.
When you are going to find city name using xpath //td[2], your are searching actually second column every time but on whole page that's why you are getting same city name every time.
You need to provide .//td[2] xpath, because when you provided xpath with . it will search only element context otherwise it will search on document means on whole page, and according to whole page output is absolutely correct.
Now if you want simply print all cities try as below :-
List<WebElement> cities = driver.findElements(By.xpath("//*[#id='content']/table/tbody//td[2]"));
for(WebElement city : cities)
{
System.out.println(city.getText());
}
Try below code to print values of the City column:
List<WebElement> rows = driver.findElements(By.xpath("//*[#id='content']/tbody/tr"));
for(int row = 0; row < rows.size(); row++){
System.out.println(driver.findElement(By.xpath("//*[#id='content']/table/tbo‌​dy/tr[row]/td[2]")).getText());
}
Hope it helps!

How to identify individual controls within a search result using Selenium Webdriver?

I have a list of search results in the following link and would like to know on how can I identify the individual controls using dynamic xpath
http://www.bigbasket.com/cl/fruits-vegetables/?nc=nb
I'm able to get the list of product names displayed using the below line
List<WebElement> productResults = browser.findElements(By.xpath("//*[contains(#id,'product')]/div[2]/span[2]/a"));
I'm able to print the product names displayed in Page 1 using the below code, but however the list size is not matching with the list of results displayed in Page 1 so which I see blank lines in between when printing
System.out.println(productResults.size());
for(int i=0;i<productResults.size();i++){
System.out.println(productResults.get(i).getText());
}
Also I tried to locate the individual controls such as Qty text box, Add button in a similar like how I located the product names but the list count is not matching so which I cannot specify the quantity, add the required product to the cart.
Could you please help me with this one?
The first step is get only the visible itens (that is displayed), sou you can use this xpath:
"//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a"
Now, you need to return the main iten div, that allows you to acess other functions. You can get the tag parents in this way:
"//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a/../../.."
The elements that you recieve in this last XPath have all html itens that you want, as set quantity, select the dropdown etc. You can acess each using a findElement() in each IWebElement of the list. Example:
List<WebElement> productResults = browser.findElements(By.xpath("//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a/../../.."));
for(WebElement element : productResults ){
IWebElement quantityInput = element.findElement(By.XPath("//input[contains(#id, '_qty')]"));
string quantityValue = quantityInput.getAttribute("value"); // if you want to know the current value. YOu can also parse it in an int
IWebElement addButton = element.findElement(By.XPath("//a[contains(#class, 'add-button')]"));
// etc to all elements inside element.
// Remember: Element is yout complete card of the item, that contains Value, name, image, buttons and all it.
}
Sorry for some Java syntax error. I am not a Java developer / tester. My piece of cake is C#.

How to find the element within element in selenium

I am creating a framework for the data validation using selenium. The issue I am struggling with is I want to locate the element "td"(html tag) within element "tr"(html tag) . This is the code I have written.
Iterator<WebElement> i = rows.iterator();
While(i.hasnext()){
List<WebElement> columns = row.findElements(By.tagName("td"));
for(WebElement s:columns)
{
System.out.println("columnDetails : "+s.getText().toString());
}
if(columns.isEmpty())
{
ElementNotFoundException e = new ElementNotFoundException("No data in table");
throw e;
}
Iterator<WebElement> j = columns.iterator();// does some other work
ClusterData c = new ClusterData(); // does some other work
ClusterDataInitializer.initUI(c, j, lheaders); // does some other work
CUIData.put(c.getCN(), c); // does some other work
}
Now the issue with this is:
I am trying to fetch the data from the rows(see table data) in arraylist and use that arraylist further. Currently whats happening is the data for column header is fetched at start of which I have no use.I only want the rows's data. I am not able to determine the proper way to collect the data of table rows only.
if xPath of the table will help you understand it properly then here are the details :
Table header xPath of cluster name column:
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/table/tbody/tr/td[2]/div/div[2]
Table row (Table Data) xPath of test cluster 01:
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[3]/div[2]/div/table/tbody/tr/td[2]/div/div/a
Please let me know if you need anything else.
I am using the following code to extract row data from table.
List<WebElement> rows = getElement(driver,sBy,"table_div_id").findElements(By.tagName("tr"));
where sBy = By.id and table_div_id = id of div in which table is present. This extracts all the rows into arraylist and then i am using code to extract the row data into another arraylist. It is where I am stuck.
Each row from the table is in its own "table" tag so following things are not working :-
List<WebElement> rows = driver.findElements(By.xpath("//div[#id = 'table_div_id']//tr"));
List<WebElement> columns = row.findElements(By.xpath("./td"));
or the approach I used for the previous release of product i.e.
List<WebElement> columns = row.findElements(By.tagName("td"));
So, I used following approach which enabled me to capture all of the visible rows from the table.
List<WebElement> columns = row.findElements(By.xpath(".//table[#class='gridxRowTable']/tbody/tr"));
But after that I faced another issue that is since this table was implemented using dojo, the scrolling was impossible and Selenium was only able to capture the visible rows , so to overcome this I zoomed out in the browser using selenium. This is how i achieved my goal of getting the data.I believe others might have provided me answer if i would have shared some more details. Still , sorry about that and hope my answer helps you all.
instead of
List<WebElement> columns = row.findElements(By.tagName("td"));
try using
List<WebElement> columns = row.findElements(By.xpath("./td"));
Check if this helps. This should give you the td elements. If I have not understood your issue, let me know.
You can use this way-
driver.findElement(By.Xpath("//table[#id=\"table1\"]/tbody/tr[2]/td[1]"));
Regards,
Anuja
Do you have selenium IDE installed? Perform storeText operation on the row you want to retrieve, then xpath will get populated in IDE. There will be multiple xpaths; the most reliable is xpath:position, use that to capture your rows.
And use firebug for better visibilty of your AUT.
Firebug and Selenium IDE are the most basic component of Selenium Framework development.
You can manipulate xpath as you want.

How can I test items with variable IDs?

I want to test variable pictures and variable check-boxes in my web page. It is like an online market, so every time you refresh the same page you will get different pictures with different IDs and check-boxes with different IDs too.
I have tried XPath, but it contains an ID so I can't use it every time with the same page:
INPUT[#name="data[car.**88535**]"]
You can actually "list" all your checkboxes with xpath.
Like :
List<WebElement> elements = driver.findElements(By.xpath("//input[#type='checkbox']"));
I think you can use the same for your pictures.
So with that you can do something like that :
void checkCB(List<WebElement> c) {
for (WebElement e : c)
if ("yes".equals(e.getAttribute("checked")))
Systeme.println("Element is checked");
}
(Currently writing with selenium 2.0)