Java 而不是在“选择”标记下获取所有选项。从我在这段代码中看到的情况来看,它打印出了所有的大小,如果选项被禁用或未被禁用,则不会考虑太多。你能发布HTML代码吗。@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑
Java 而不是在“选择”标记下获取所有选项。从我在这段代码中看到的情况来看,它打印出了所有的大小,如果选项被禁用或未被禁用,则不会考虑太多。你能发布HTML代码吗。@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑,java,selenium,selenium-webdriver,selenium-chromedriver,selenium-ide,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Selenium Ide,而不是在“选择”标记下获取所有选项。从我在这段代码中看到的情况来看,它打印出了所有的大小,如果选项被禁用或未被禁用,则不会考虑太多。你能发布HTML代码吗。@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑中,它就工作了。谢谢你的帮助。你能发布HTML代码吗?@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑中,它就工作了。谢谢你的帮助。 public class FootlockerExampl
而不是在“选择”标记下获取所有选项。从我在这段代码中看到的情况来看,它打印出了所有的大小,如果选项被禁用或未被禁用,则不会考虑太多。你能发布HTML代码吗。@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑中,它就工作了。谢谢你的帮助。你能发布HTML代码吗?@ShubhasmitGupta我在Marius D的帮助下解决了这个问题。将他的代码添加到我的逻辑中,它就工作了。谢谢你的帮助。
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();
}
Select select = new Select(driver.findElement(By.id("product_sizes")));
List<WebElement> availableSizes = select.getOptions();
for (WebElement size : availableSizes) {
System.out.println(size.getText());
}
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.");
}