I am trying to verify selected item is exists in another (Cart/Shopping Bag) page, for EX: I am buying one/many items from application and then I will move all the items to Cart/Shopping Bag, here I want to verify the previously selected item is same as in your Cart/Shopping Bag.

Please let me know how to compare items in both/different the pages.

Test URL is: http://www.myntra.com/

Blow are the code sample and screenshots.

    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.*;
    import org.testng.annotations.Test;

    public class CartListInMyntra {
    static WebDriver driver = new FirefoxDriver();
    public void method() {
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement ele =wait.until(ExpectedConditions.elementToBeClickable(By.id("q")));
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebElement table = driver.findElement(By.className("results-cnt"));// 
    List<WebElement> rows=table.findElements(By.className("product"));
    int count = rows.size();
    for (WebElement items : rows){

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[4]/ul/li[1]"));     

WebElement viewCart = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/a[3]"));

  WebElement view = driver.findElement(By.className("prod-name"));
  List<WebElement>selected_Items =view.findElements(By.tagName("a"));       
  for (WebElement sel_items : selected_Items){
  System.err.println("NOT EQUALS");

Compare 01

Compare 02


You are comparing values, so better you store each values in the String and compare both the String values.

| improve this answer | |

I think the easiest way to do this would be to manipulate the javascript on the page to get the two strings you need. First go to that primary shopping page and have the browser click the quick view this will load and give you a way to select the text use document.querySelector('h1 > a').textContent; and you should get something like "Roadster Men White Printed T-shirt " you may need to trim the trailing white space and store in a variable. then add it to your cart and on the next page use document.querySelector('div.prod-name > a').textContent; then compare your two strings.

| improve this answer | |
  • I am unable to do this (Jquery), if possible could you please give me the query. If possible. – QA4it Sep 13 '14 at 8:28
  • 2
    Selenium has a javascript executor otherwise use something else like casperjs it's easier to execute javascript on the page. dropbox.com/s/f5o9esbw0fn0n0k/demo.mov?dl=0 – alanmbarr Sep 14 '14 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.