0

I have a code that invokes chrome driver and then goes to footlocker's website. After opening footlocker's website it finds and clicks on the Mens button. Then it goes through a list of products under men and selects one at random. The problem I am having is that it selects the same product each time. Here is my code. The method for selecting a random product is under selectRandomProduct

public class FootlockerExample {

WebElement next;
WebDriver driver = new ChromeDriver();

public void productOne (){

    // Open Chrome Browser
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Working\\Workspace\\SeleniumProject\\chromedriver.exe");

    // Open Footlocker website and maximize window
    driver.get("http://www.footlocker.ca/");
    driver.manage().window().maximize();

    // Find button element 'Mens' and click
    next = driver.findElement(By.xpath("//*[@id='global-nav']/ul/li[1]/a"));
    next.click();

    // Select a random product
    selectRandomProduct();

    // Print out the product name and price
    String productName = driver.findElement(By.xpath("//*[@id='product_form']/div/span[2]/div/div[1]")).getText();
    String Price = driver.findElement(By.xpath("//*[@id='product_form']/div/span[2]/div/div[2]")).getText(); 
    System.out.println("The 1st random product is " + productName + " and it's cost is " + Price + ".");

    // Execute new method
    productTwo();
}

public void productTwo(){

    // Go back a browser page
    driver.navigate().back();
    selectRandomProduct();

    // Print out the product name and price
    String productName = driver.findElement(By.xpath("//*[@id='product_form']/div/span[2]/div/div[1]")).getText();
    String Price = driver.findElement(By.xpath("//*[@id='product_form']/div/span[2]/div/div[2]")).getText(); 
    System.out.println("The 2nd random product is " + productName + " and it's cost is " + Price + ".");
}

public void selectRandomProduct(){

    // Find and click on a random product
    List<WebElement> allProducts = driver.findElements(By.xpath("//*[@id='endecaResultsWrapper']/div[3]"));
    Random rand = new Random();
    int randomProduct = rand.nextInt(allProducts.size());
    allProducts.get(randomProduct).click();
}

public static void main(String[] args) {

    FootlockerExample obj1 = new FootlockerExample();
    obj1.productOne();
}

}

2
  • 1
    Could you provide a link to the footlocker website? They seem to have different page layouts for various countries. In the meantime, can you check if allProducts.size() > 1 please? Commented Dec 27, 2016 at 0:08
  • Sure the website I'm using is for Canada. footlocker.ca. How would I go about to perform a check to see if allProducts.size() is >1? I am fairly new to Selenium. Commented Dec 27, 2016 at 2:09

1 Answer 1

1

I looked at the website and I found out that your xpath (//*[@id='endecaResultsWrapper']/div[3]) selects the whole div where there are all the images. So basically, when you click on a random element, it only finds one (the main div). If you want to click on one of the 60 products, you should try something like this: //*[@id='endecaResultsWrapper']/div[3]//img.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That seems to have fixed the issue and is now picking different products each time.Thank you once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.