我想添加5秒的等待时间并运行下一行
driver.findElement(By.xpath("html/body/div[2]/bookking-
navbar/nav/div/div/div[1]/div[3]/ul/li[1]/authentication/a/span")).click();
String value= driver.findElement(By.xpath(".//*
[@id='username']")).getText();
这里我要添加5秒等待然后运行下一行如果使用thread.sleep(500)网页加载太多时间
driver.findElement(By.xpath(".//*[@id='username']")).clear();
driver.findElement(By.xpath(".//*[@id='username']")).sendKeys(value);
driver.findElement(By.xpath(".//*[@id='password']")).sendKeys("apple123");
driver.findElement(By.xpath("html/body/div[1]/div/divdiv/button")).click();
答案 0 :(得分:3)
使线程睡眠:
的Thread.sleep(5000);
但这不是一个很好的决定来做这样的睡眠,因为你的测试会很慢。使用webdriver代替:
WebDriverWait wait = new WebDriverWait(driver,5000); wait.until(ExpectedConditions.elementToBeClickable(By.xpath(" xpathHere&#34)));
它将等到elementToBeClickable
(或者您可以选择任何其他条件)并继续执行测试。否则,如果在超时期间未满足条件 - 将抛出异常。
<强>更新强> 如果您使用selenium + java编写UI回归测试,强烈建议您使用Selenide framework。这是10分钟的开始教程:https://vimeo.com/107647158
WebDriverRunner.setWebDriver(driver);
传递WebDriver设置)$("#elementId").pressEnter().should(disappear);
$(By.id("elId")).waitUntil(attribute("attr", "expectedValued"), 5000); $(By.id("elId")).should(matchText("Text to match"));
初始代码:
driver.findElement(By.xpath(".//*[@id='username']")).clear();
driver.findElement(By.xpath(".//*[@id='username']")).sendKeys(value);
driver.findElement(By.xpath(".//*[@id='password']")).sendKeys("apple123");
driver.findElement(By.xpath("html/body/div[1]/div/divdiv/button")).click();
Selenide看起来像(包括所有等待):
$("#username").shouldBe(visible).setValue(value);
$("#password").shouldBe(visible).setValue("apple123");
$(By.xpath("html/body/div[1]/div/divdiv/button")).shouldBe(visible).setValue("apple123");