Dies ist, was mein Code tut. (1.) Öffnen Sie Chrome-Browser und gehen (2.) Klicken Sie auf die Schaltfläche Herren (3.) Wählen Sie 1 zufällige Produkt aus der Liste der 60 (4.) Drucken Sie den Produktnamen und Preis footlocker.ca (5.) Drucken Sie alle verfügbaren Größen für das ausgewählte Produkt (AvaSizes) (6.) Zurück zur Produktseite (7.) Wählen Sie ein 2. zufälliges Produkt aus der Liste 60 (8.) Drucken Sie den Produktnamen und Preis (9.) Drucken Sie alle verfügbaren Größen für das ausgewählte Produkt (AvaSizes) (10.) Schließen Chrom BrowserAlle aktivierten Objekte innerhalb einer Liste drucken
Mein Problem ist, dass es nicht die verfügbaren Größen für das Produkt zu lesen. Ich denke, dass mein Problem in dem XPath ist, aber ich bin nicht sicher, da ich mit verschiedenen XPaths gebastelt habe, so ist es vielleicht mein Code, der das Problem ist. Die Methode heißt (avaSizes). Wenn jemand helfen kann, wäre es großartig. Ich tue dies für die Praxis, also wenn jemand einige Echtzeit-Job-Szenario-Testfälle hat, die ich zu diesem Code hinzufügen könnte, würde es mir sehr helfen. Vielen Dank.
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();
}
}
können Sie den HTML-Code veröffentlichen. –
@ShubhasmitGupta Ich habe das Problem mit Hilfe von Marius D. behoben. Ich habe seinen Code zu meiner Logik hinzugefügt und es funktioniert. Danke aber dafür, dass du versucht hast zu helfen. –