Selenium Automation脚本实践中嵌套for循环不自增
Nested for loop not incrementing in the Selenium Automation script practice
有一个 div 的网站,包含 7 件商品,当一件商品添加到购物车时,会出现一个关于继续购物或结帐的新弹出窗口。需要自动化,在点击继续购物的同时将所有 7 件商品一件一件添加。
我用嵌套的 For 循环试过这个,但看起来嵌套的 for 循环变量“j”没有递增。
目前,对于第一个项目,按预期工作,但对于其余项目,仅执行第一个循环的鼠标悬停操作。
还请指点有没有比我的方法更好的方法,不胜感激
代码:
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DressShop {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:/Java with Lan/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.get("http://automationpractice.com/index.php");
List<WebElement> offers = driver.findElements(By.cssSelector("#homefeatured li"));
List<WebElement> addCarts = driver.findElements(By.xpath("//ul[@id='homefeatured']/li/div/div[2]/div[2]/a[1]"));
Actions a = new Actions(driver);
for (int i = 0; i < offers.size(); i++) {
WebElement offer = offers.get(i);
a.moveToElement(offer).build().perform();
for (int j = 0; j == i; j++) {
WebElement addCart = addCarts.get(j);
wait.until(ExpectedConditions.elementToBeClickable(addCart));
a.moveToElement(addCart).click().build().perform();
WebElement closePop = driver.findElement(By.xpath("//span[@title='Continue shopping']"));
wait.until(ExpectedConditions.elementToBeClickable(closePop));
a.moveToElement(closePop).click().build().perform();
}
}
}
}
对于这种情况,您真的不需要嵌套循环。
你可以收集所有link
//ul[@id='homefeatured']//li
使用此 xpath,现在遍历列表并将鼠标悬停在每个产品上并单击添加到购物车,然后继续购物。
代码:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:/Java with Lan/chromedriver.exe");
WebDriver driver = new ChromeDriver();
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
Actions action = new Actions(driver);
List<WebElement> allLinks = driver.findElements(By.xpath("//ul[@id='homefeatured']//li"));
for (WebElement link : allLinks) {
action.moveToElement(wait.until(ExpectedConditions.visibilityOf(link))).pause(3).build().perform();
WebElement addToCart = link.findElement(By.xpath(".//descendant::div[@class='right-block']//descendant::a[@title='Add to cart']"));
action.moveToElement(addToCart).pause(2).click().build().perform();
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//descendant::div[@class='right-block']//descendant::a[@title='Add to cart']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@title='Continue shopping']"))).click();
}
有一个 div 的网站,包含 7 件商品,当一件商品添加到购物车时,会出现一个关于继续购物或结帐的新弹出窗口。需要自动化,在点击继续购物的同时将所有 7 件商品一件一件添加。
我用嵌套的 For 循环试过这个,但看起来嵌套的 for 循环变量“j”没有递增。
目前,对于第一个项目,按预期工作,但对于其余项目,仅执行第一个循环的鼠标悬停操作。
还请指点有没有比我的方法更好的方法,不胜感激
代码:
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DressShop {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:/Java with Lan/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.get("http://automationpractice.com/index.php");
List<WebElement> offers = driver.findElements(By.cssSelector("#homefeatured li"));
List<WebElement> addCarts = driver.findElements(By.xpath("//ul[@id='homefeatured']/li/div/div[2]/div[2]/a[1]"));
Actions a = new Actions(driver);
for (int i = 0; i < offers.size(); i++) {
WebElement offer = offers.get(i);
a.moveToElement(offer).build().perform();
for (int j = 0; j == i; j++) {
WebElement addCart = addCarts.get(j);
wait.until(ExpectedConditions.elementToBeClickable(addCart));
a.moveToElement(addCart).click().build().perform();
WebElement closePop = driver.findElement(By.xpath("//span[@title='Continue shopping']"));
wait.until(ExpectedConditions.elementToBeClickable(closePop));
a.moveToElement(closePop).click().build().perform();
}
}
}
}
对于这种情况,您真的不需要嵌套循环。
你可以收集所有link
//ul[@id='homefeatured']//li
使用此 xpath,现在遍历列表并将鼠标悬停在每个产品上并单击添加到购物车,然后继续购物。
代码:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:/Java with Lan/chromedriver.exe");
WebDriver driver = new ChromeDriver();
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
Actions action = new Actions(driver);
List<WebElement> allLinks = driver.findElements(By.xpath("//ul[@id='homefeatured']//li"));
for (WebElement link : allLinks) {
action.moveToElement(wait.until(ExpectedConditions.visibilityOf(link))).pause(3).build().perform();
WebElement addToCart = link.findElement(By.xpath(".//descendant::div[@class='right-block']//descendant::a[@title='Add to cart']"));
action.moveToElement(addToCart).pause(2).click().build().perform();
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//descendant::div[@class='right-block']//descendant::a[@title='Add to cart']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@title='Continue shopping']"))).click();
}