0

这就是我的代码所做的。 (2.)点击男装按钮 (3.)从60列表中选择1个随机产品(4.)打印产品名称和价格 (1.)打开产品名称和价格 (1.)打开浏览器并转到footlocker.ca (2.)点击男装按钮 (5.)打印所有产品的所有可用尺寸(avaSizes) (6.)返回到产品页面 (7.)从列表中选择第二个随机产品(012)(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(); 
} 

}

+0

你可以发布HTML代码吗? –

+0

@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到了我的逻辑中,并且工作正常。非常感谢您的帮助。 –

回答

1

从马吕斯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."); 
} 
4

我看到下拉元素有一个ID,并进行类型选择的。 您可以使用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()); 
    } 
+0

我需要它只打印启用的大小。 ShoeA的尺寸为9,10,11,12,13,但只有10或12的尺寸可供选择,所以它应该打印10,12,而不是在Select标签下获得所有选项。根据我在代码中看到的内容,它会打印出所有尺寸,如果该选项被禁用,则不会考虑在内。 –