Exception on trying to set Quantity inwebuy.com excercise:
=============================================
sir i tried sendkeys.Did not work.So,tried select.
Following is code snippet:
wi=Quantity_List_page.get(k);
Select s=new Select(wi);
Following is complete code:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
public class shopping_cart_c {
static Xls_Reader x=new Xls_Reader("C:\\Users\\chaitra\\Pictures\\shopping_cart.xlsx");
static String QuantityL_xl;
static WebElement wi;
static String PN_xl;
static String[]Q_a=new String[6];
public static void main(String[] args) {
// TODO Auto-generated method stub
ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("Chaitra");
FirefoxDriver w = new FirefoxDriver(p);
//go to the page
w.navigate().to("https://uk.webuy.com/search/index.php?stext=*§ion=&catid=829");
w.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
//create list to extract webelements-[albums & respective buttons]
List<WebElement>L_albums=new ArrayList<WebElement>();
L_albums=w.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
System.out.println("L_albums_____Size"+L_albums.size());
List<WebElement>Buttons_on_page=new ArrayList<WebElement>();
Buttons_on_page=w.findElements(By.partialLinkText("I want to buy this item"));
System.out.println("Size2_Buttons_on_page"+Buttons_on_page.size());
//reading xlsx
for(int i=1;i<6;i++)
{
PN_xl=x.getCellData("Products", "Product_names", i+1);
String Quantity=x.getCellData("Products", "Quantity",i+1);
Q_a[i-1]=Quantity;
System.out.println("PN_xl-------"+PN_xl+"---------Quantity"+Quantity);
//See if contents of xls match the albums in page--if yes--click to buy it
for(int j=0;j<L_albums.size();j++)
{
System.out.println("2ndloop--PN_xl :"+PN_xl+"---------data in list: "+ L_albums.get(j).getText());
if(PN_xl.contains((L_albums.get(j).getText())))//(L.get(j).getText())
{
Buttons_on_page.get(j).click();
w.navigate().back();
System.out.println("have added product to cart");
System.out.println("-------------------------------------------------------------------");
break;
}
}
}
//to check cart
w.findElement(By.xpath("//td[@class='basketTableCellLnk']/a")).click();
//Setting quantity for the products in cart
for(int k=0;k<L_albums.size();k++)
{
}
//put all data from Quntity column at page into a list
List<WebElement>Quantity_List_page=new ArrayList<WebElement>();
Quantity_List_page=w.findElements(By.xpath("//*[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
System.out.println("Qunatity_size_from cart page"+Quantity_List_page.size());
//contents of Q_a[i-1] array which has quantity specified as mentioned in xlsx
for(int j=4;j>=0;j--)
{
System.out.println("-----------Q_a[i]-------"+Q_a[j]);
}
Select s;
//Select s=new Select(wi);
//trying to set quantity in cart as per xls
//============================================================================================
for(int k=4;k>=0;k--)
{
wi=Quantity_List_page.get(k);
wi.sendKeys(Q_a[k]);//<========================this code works only once=====================*/
s=new Select(wi);//<============staleElementReferenceException==========================>
s.selectByVisibleText(Q_a[k]);
System.out.println("Am setting quantity"+Q_a[k]);
}
//====================================================================================
}
}
to solve stale element refrence exception
please look at these q/a
http://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium
http://stackoverflow.com/questions/16166261/selenium-webdriver-stale-element-reference-exception
Sir,
resolved staleElement exception now ,by redeclaring/reassigning alll variables.
Still i face the same issue. only 1st product's quantity can be set.
I compare my code to the code from website. [No difference in code....same code]
Please find my code:
===================
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import junit.framework.Assert;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
public class shopping_cart_c {
static Xls_Reader x=new Xls_Reader("C:\\Users\\chaitra\\Pictures\\shopping_cart.xlsx");
static String QuantityL_xl;
static WebElement wi;
static String Quantity_xl;
static int count=0;
static String PN_xl;
static String[]Q_a=new String[6];
public static void main(String[] args) {
// TODO Auto-generated method stub
ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("Chaitra");
FirefoxDriver w = new FirefoxDriver(p);
//go to the page
w.navigate().to("https://uk.webuy.com/search/index.php?stext=*§ion=&catid=829");
w.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
//create list to extract webelements-[albums & respective buttons]
List<WebElement>L_albums=new ArrayList<WebElement>();
L_albums=w.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
System.out.println("L_albums_____Size"+L_albums.size());
List<WebElement>Buttons_on_page=new ArrayList<WebElement>();
Buttons_on_page=w.findElements(By.partialLinkText("I want to buy this item"));
System.out.println("Size2_Buttons_on_page"+Buttons_on_page.size());
//reading xlsx
for(int i=1;i<6;i++)
{
PN_xl=x.getCellData("Products", "Product_names", i+1);
String Quantity=x.getCellData("Products", "Quantity",i+1);
Q_a[i-1]=Quantity;
System.out.println("PN_xl-------"+PN_xl+"---------Quantity"+Quantity);
WebElement Read_cart;
//See if contents of xls match the albums in page--if yes--click to buy it
for(int j=0;j<L_albums.size();j++)
{
System.out.println("2ndloop--PN_xl :"+PN_xl+"---------data in list: "+ L_albums.get(j).getText());
if(PN_xl.contains((L_albums.get(j).getText())))//(L.get(j).getText())
{
count++;
Buttons_on_page.get(j).click();
//w.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//Read_cart=w.findElement(By.xpath("//td[@class='basketTableCell']"));
//w.navigate().back();
//System.out.println("Currently der r so n so in cart--"+Read_cart.getText());
w.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
Read_cart=w.findElement(By.xpath("//span[@id='buyBasketCount']"));
String x1=Read_cart.getText();
System.out.println("Currently der r so n so in cart--"+x1);
//Assert.assertEquals(x1, String.valueOf(j+1));
//System.out.println("Result"+x1.compareTo(String.valueOf(j+1)));
int h=j+1;
System.out.println("comapring count"+(count)+"to"+x1);
w.navigate().back();
System.out.println("-------------------------------------------------------------------");
break;
}
}
}
///////////////////////////////*
//to click cart
w.findElement(By.xpath("//td[@class='basketTableCellLnk']/a")).click();
/*
//Setting quantity for the products in cart
for(int k=0;k<L_albums.size();k++)
{
}
//put all data from Quntity column at page into a list
List<WebElement>Quantity_List_page=new ArrayList<WebElement>();
Quantity_List_page=w.findElements(By.xpath("//*[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
System.out.println("Qunatity_size_from cart page"+Quantity_List_page.size());
//contents of Q_a[i-1] array which has quantity specified as mentioned in xlsx
for(int j=4;j>=0;j--)
{
System.out.println("-----------Q_a[i]-------"+Q_a[j]);
}
//Select s;
int k=0;
wi=Quantity_List_page.get(k);
Select s=new Select(wi);*//////////////////////////
/*//trying to set quantity in cart as per xls
//============================================================================================
for(k=4;k>=0;k--)
{
//wi=Quantity_List_page.get(k);
//s=new Select(wi);//<============staleElementReferenceException==========================>
s.selectByVisibleText(Q_a[k]);
System.out.println("Am setting quantity"+Q_a[k]);
w.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebElement wi=Quantity_List_page.get(k-1);
s=new Select(wi);
}*/
//====================================================================================
//put all the data collected from xcel in hashtable
Hashtable<String,String>H=new Hashtable<String,String>();
for(int i=1;i<6;i++)
{
PN_xl=x.getCellData("Products", "Product_names", i+1);
Quantity_xl=x.getCellData("Products", "Quantity",i+1);
H.put(PN_xl, Quantity_xl);
System.out.println("PN_xl"+PN_xl+"hash "+H.get(PN_xl));
}
//print all contents of hashtable:
System.out.println("h size"+H.size());
System.out.println("hash "+H.get(PN_xl));
// read data from cart page,match d same product name from map & set the quantity as per map in cart page
List<WebElement>Product_name_cart=new ArrayList<WebElement>();
Product_name_cart=w.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[2]"));
System.out.println("-----------------------------------------------------");
System.out.println("product names_size_from cart page"+Product_name_cart.size());
//read quantity from cart page.set as per compariso n wid hashtable
List<WebElement>Quantity_List_page=new ArrayList<WebElement>();
Quantity_List_page=w.findElements(By.xpath("//*[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
System.out.println("-----------------------------------------------------");
System.out.println("Qunatity_size_from cart page"+Quantity_List_page.size());
//check if data from cart page[product name] matches with that in map
//================================================================
//code where loop works only once & does not execute the 2nd time
//=========================================================================
for(int i=0;i<Product_name_cart.size()-2;i++)
{
System.out.println("=======================================================");
//System.out.println("content"+Product_name_cart.get(i).getText());
String key=Product_name_cart.get(i).getText().split("\\n")[0];
System.out.println("key"+key);
System.out.println("=======================================================");
System.out.println("checking match--- key"+key+"-----Quantity"+H.get(key));
WebElement we=Quantity_List_page.get(i);
Select s=new Select(we);
s.selectByVisibleText(H.get(key));
Product_name_cart=w.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[2]"));
Quantity_List_page=w.findElements(By.xpath("//*[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
}
//=================================================
Above loop works only once...n does not proceed further....
//================================================
}
}