打印列表中所有启用的项目
Print all enabled items within a list
这就是我的代码所做的。
(1.) 打开 chrome 浏览器并转到 footlocker.ca
(2.) 单击男士按钮
(3.) Select 60 个列表中的 1 个随机产品
(4.) 打印产品名称和价格
(5.) 打印所选产品的所有可用尺寸 (avaSizes)
(6.) 返回产品页面
(7.) Select 60 个列表中的第二个随机产品
(8.) 打印产品名称和价格
(9.) 打印所选产品的所有可用尺寸 (avaSizes)
(10.) 关闭 chrome 浏览器
我的问题是它无法读取产品的可用尺寸。我认为我的问题出在 xpath 中,但我不确定,因为我修改了各种 xpath,所以问题可能出在我的代码上。该方法称为 (avaSizes)。如果有人可以提供帮助,那就太好了。我这样做是为了练习,所以如果有人有一些实时工作场景测试用例可以添加到这段代码中,那将对我有很大帮助。谢谢
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 randomly selected product is " + productName + " and it's cost is " + Price + ".");
// Print all available product sizes
avaSizes();
// Execute new method
productTwo();
}
public void productTwo(){
// Go back a browser page
driver.navigate().back();
// Select a new 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 2nd randomly selected product is " + productName + " and it's cost is " + Price + ".");
// Print all available product sizes
avaSizes();
driver.close();
}
public void selectRandomProduct(){
// Find and click on a random product
List<WebElement> allProducts = driver.findElements(By.xpath("//*[@id='endecaResultsWrapper']/div[3]//img"));
Random rand = new Random();
int randomProduct = rand.nextInt(allProducts.size());
allProducts.get(randomProduct).click();
}
public void avaSizes(){
// Find all the available shoe sizes for each randomly selected product
List<WebElement> avaSizes = driver.findElements(By.xpath("//*[@id='product_sizes']"));
int totalSizes = 0;
for(int i=0; i<avaSizes.size(); i++){
if(avaSizes.get(i).isEnabled()==true){
avaSizes.get(i).getText();
System.out.println(avaSizes);
totalSizes++;
}else{
System.out.println("Out of stock in all sizes.");
}
}
System.out.println("This product is available in: " + totalSizes + " sizes.");
}
public static void main(String[] args) {
FootlockerExample obj1 = new FootlockerExample();
obj1.productOne();
}
}
我看到下拉元素有一个 id 并且是 Select 类型。
您可以使用 Select 对象来处理它:
Select select = new Select(driver.findElement(By.id("product_sizes")));
List<WebElement> availableSizes = select.getOptions();
for (WebElement size : availableSizes) {
System.out.println(size.getText());
}
在上面 Marius D 的帮助下,我找到了问题并修复了我的代码。
这是我未来的固定答案,以防有人遇到同样的问题。
public void avaSizes(){
Select select = new Select(driver.findElement(By.id("product_sizes")));
// Find all the available shoe sizes for each randomly selected product
List<WebElement> avaSizes = select.getOptions();
int totalSizes = 0;
for(WebElement size:avaSizes){
if(size.isEnabled()==true){
System.out.println(size.getText());
totalSizes++;
}else{
System.out.println("Out of stock in " + size.getText());
}
}
System.out.println("This product is available in: " + totalSizes + " sizes.");
}
这就是我的代码所做的。 (1.) 打开 chrome 浏览器并转到 footlocker.ca (2.) 单击男士按钮 (3.) Select 60 个列表中的 1 个随机产品 (4.) 打印产品名称和价格 (5.) 打印所选产品的所有可用尺寸 (avaSizes) (6.) 返回产品页面 (7.) Select 60 个列表中的第二个随机产品 (8.) 打印产品名称和价格 (9.) 打印所选产品的所有可用尺寸 (avaSizes) (10.) 关闭 chrome 浏览器
我的问题是它无法读取产品的可用尺寸。我认为我的问题出在 xpath 中,但我不确定,因为我修改了各种 xpath,所以问题可能出在我的代码上。该方法称为 (avaSizes)。如果有人可以提供帮助,那就太好了。我这样做是为了练习,所以如果有人有一些实时工作场景测试用例可以添加到这段代码中,那将对我有很大帮助。谢谢
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 randomly selected product is " + productName + " and it's cost is " + Price + ".");
// Print all available product sizes
avaSizes();
// Execute new method
productTwo();
}
public void productTwo(){
// Go back a browser page
driver.navigate().back();
// Select a new 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 2nd randomly selected product is " + productName + " and it's cost is " + Price + ".");
// Print all available product sizes
avaSizes();
driver.close();
}
public void selectRandomProduct(){
// Find and click on a random product
List<WebElement> allProducts = driver.findElements(By.xpath("//*[@id='endecaResultsWrapper']/div[3]//img"));
Random rand = new Random();
int randomProduct = rand.nextInt(allProducts.size());
allProducts.get(randomProduct).click();
}
public void avaSizes(){
// Find all the available shoe sizes for each randomly selected product
List<WebElement> avaSizes = driver.findElements(By.xpath("//*[@id='product_sizes']"));
int totalSizes = 0;
for(int i=0; i<avaSizes.size(); i++){
if(avaSizes.get(i).isEnabled()==true){
avaSizes.get(i).getText();
System.out.println(avaSizes);
totalSizes++;
}else{
System.out.println("Out of stock in all sizes.");
}
}
System.out.println("This product is available in: " + totalSizes + " sizes.");
}
public static void main(String[] args) {
FootlockerExample obj1 = new FootlockerExample();
obj1.productOne();
}
}
我看到下拉元素有一个 id 并且是 Select 类型。 您可以使用 Select 对象来处理它:
Select select = new Select(driver.findElement(By.id("product_sizes")));
List<WebElement> availableSizes = select.getOptions();
for (WebElement size : availableSizes) {
System.out.println(size.getText());
}
在上面 Marius D 的帮助下,我找到了问题并修复了我的代码。 这是我未来的固定答案,以防有人遇到同样的问题。
public void avaSizes(){
Select select = new Select(driver.findElement(By.id("product_sizes")));
// Find all the available shoe sizes for each randomly selected product
List<WebElement> avaSizes = select.getOptions();
int totalSizes = 0;
for(WebElement size:avaSizes){
if(size.isEnabled()==true){
System.out.println(size.getText());
totalSizes++;
}else{
System.out.println("Out of stock in " + size.getText());
}
}
System.out.println("This product is available in: " + totalSizes + " sizes.");
}