Select Selenium Webdriver 列表中的随机元素 Java
Select a random element from a list in Selenium Webdriver Java
我有一个调用 chrome 驱动程序的代码,然后转到 footlocker 的网站。打开 footlocker 的网站后,它会找到并点击 Mens 按钮。然后它会浏览男士产品列表并随机选择一个。我遇到的问题是它每次都选择相同的产品。这是我的代码。选择随机产品的方法在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();
}
}
我查看了该网站,发现您的 xpath (//*[@id='endecaResultsWrapper']/div[3]
) 选择了包含所有图像的整个 div。所以基本上,当你点击一个随机元素时,它只会找到一个(主要 div)。如果你想点击 60 种产品中的一种,你应该尝试这样的操作://*[@id='endecaResultsWrapper']/div[3]//img
.
我有一个调用 chrome 驱动程序的代码,然后转到 footlocker 的网站。打开 footlocker 的网站后,它会找到并点击 Mens 按钮。然后它会浏览男士产品列表并随机选择一个。我遇到的问题是它每次都选择相同的产品。这是我的代码。选择随机产品的方法在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();
}
}
我查看了该网站,发现您的 xpath (//*[@id='endecaResultsWrapper']/div[3]
) 选择了包含所有图像的整个 div。所以基本上,当你点击一个随机元素时,它只会找到一个(主要 div)。如果你想点击 60 种产品中的一种,你应该尝试这样的操作://*[@id='endecaResultsWrapper']/div[3]//img
.