【发布时间】:2020-09-01 17:44:51
【问题描述】:
我正在尝试使用 java/selenium 的 for 循环多次运行以删除电子邮件,但它只运行一次。我仍然是这方面的初学者,无法弄清楚我做错了什么。有人可以为我提供一些帮助吗?非常感谢您的帮助。下面是我正在运行的代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class deleteYahooEmail {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "/Users/lena/WebDrivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver(); //launches the browser
driver.get("https://login.yahoo.com/");
Thread.sleep(2000);
driver.findElement(By.id("login-username")).sendKeys("someemailhere@yahoo.com");
Thread.sleep(2000);
driver.findElement(By.id("login-signin")).click();
Thread.sleep(2000);
driver.findElement(By.id("login-passwd")).sendKeys("passwrdhere");
Thread.sleep(2000);
driver.findElement(By.id("login-signin")).click();
Thread.sleep(2000);
driver.findElement(By.id("header-mail-button")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
for (int i = 0; i >10; i = i + 1) {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='button' and @data-test-id='checkbox']"))).click();
driver.findElement(By.xpath("//button[@type='button' and @data-test-id='toolbar-delete']")).click();
}
}
}
【问题讨论】:
-
您的 for 循环不正确。一个for循环由3个部分组成,第一个是初始化
int i = 0;2. 条件i < 10;基本上是说当i小于10时,继续执行for循环的主体。 3. 每次循环迭代后进行递增/递减,i ++最终您的i将 = 10,您的循环将不再执行。所以你的循环应该如下所示:for(int i = 0; i < 10; i ++) { do something } -
我将此更改为建议的方法,但没有任何反应。 for(int i = 0; i
-
你试过调试器吗?我不熟悉你的代码,所以很遗憾我不能再进一步了。祝你好运!
-
我是新手,需要学习调试器。我确实提供了完整的代码,您只需要添加自己的用户名和密码。不过感谢您的尝试!
-
我也尝试过使用 for - while 循环,但代码只运行一次,也不是连续循环:int x = 1; while (x
标签: java eclipse selenium for-loop automation