简体   繁体   中英

Select a random element from a list in Selenium Webdriver Java

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();
}

}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM