2

I am having difficulty accessing an edit box element that is within a web table. This is my code:

package cartsAndCheckout;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import utility.Base;
import utility.Cart_Functions;

public class ModifyCart {

    public static void main(String[] args) throws InterruptedException {

        //search for product, add to cart
        Base.openURL();
        Cart_Functions.productToCart();

        //navigate to cart
        Cart_Functions.NavigateToCart();

        //Change quantity
        try {
            Thread.sleep(10000);
            Base.driver.findElement(By.cssSelector("#cart-444-qty")).sendKeys("5");
        }
        catch (NoSuchElementException e)
        {
            System.out.println("There was an error: 'No such element'");
        }

}

}

I have selected the locator for the edit box, but it is throwing a 'locator not found' error, presumably because it's within a web table. Can someone please help?

R.Donohoe
  • 59
  • 8
  • My guess is that it's in an IFRAME. Can you post a URL to the page? – JeffC Nov 27 '18 at 20:42
  • I have tried switching to default frame, which didn't work (no effect), and to frames 0 and 1, but this didn't work ('no such frame'). The dev environment I'm working in is internally restricted but this is the live version: https://heelys.com/cart – R.Donohoe Nov 28 '18 at 10:27
  • (it's the quantity edit box I'm trying to access) – R.Donohoe Nov 28 '18 at 10:33

1 Answers1

0

After looking at the public URL that you posted, I added two products to the cart but couldn't find the ID that you were looking for or really any form of it so I'm not sure where you got it from. There's also no IFRAME like I had thought but the element is present and can be located.

When I do things like this, I write functions because I know I'm going to use this code repeatedly. I would write a function that takes some parameters that allow me to locate the product that I want from the cart and then edit the Qty. There's several ways you could do this... take in the product info (product name, color, size, etc.) and the desired Qty is one way, take in an index (of the product in the table) and the desired Qty is another.

I'm going to be lazy and do the easy one... specify an index and desired Qty just to demonstrate one way.

public void setQty(int index, int qty)
{
    driver.findElements(By.cssSelector("table.cart tr td.quantity > input")).get(index).sendKeys(qty.toString());
}

This locator gets all the INPUTs inside of a TD for quantity inside of the cart TABLE. It uses the index into the table row, e.g. 0 for first row, 1 for second, and so on.

To use this, you would write something like

setQty(0, 2);

to set the quantity of the first product in the cart to 2 items.

I don't have an IDE so there may be some typos, etc. but that should get you started.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Hi, thanks for your help and sorry for long reply I've been super busy. I'm getting a syntax error on 'qty.toString' - cannot invoke toString() on the primitive type int'. Not sure how to resolve this? – R.Donohoe Nov 30 '18 at 16:15
  • See [this](https://stackoverflow.com/questions/4105331/how-do-i-convert-from-int-to-string) – JeffC Nov 30 '18 at 19:01