這就是我的代碼所做的。 (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();
}
}
你可以發佈HTML代碼嗎? –
@ShubhasmitGupta我在Marius D的幫助下解決了這個問題。將他的代碼添加到了我的邏輯中,並且工作正常。非常感謝您的幫助。 –