【问题标题】:Need to write the selenium code without the Thread.sleep需要编写没有 Thread.sleep 的 selenium 代码
【发布时间】:2017-03-08 08:20:15
【问题描述】:

我已经编写了以下代码来登录网站"qtpselenium.com"

如果我在两者之间设置 Thread.sleep 以使代码执行暂停一段时间,则以下代码可以正常工作。 如果我评论 Thread.sleep,代码将无法按预期工作。 我尝试使用 selenium 的隐式和显式等待来使驱动程序等待元素可见,但代码仅在我使用 Thread.sleep 时按预期工作。

有什么方法可以让下面的代码在不使用 Thraed.Sleep 语句的情况下工作。

在 selenium 代码中使用 Thread.sleep 语句是一种不好的做法吗?

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

public class QTPSelenium {

    public static WebDriver driver = null;

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://qtpselenium.com/");

        driver.findElement(By.xpath(".//*[@class='btn btn-default member_login']")).click();
        Thread.sleep(10000);            

        driver.findElement(By.xpath("(//button[@type='submit'])[3]")).click();
        Thread.sleep(10000);

        driver.findElement(By.id("email")).sendKeys("Some Email ID");
        driver.findElement(By.id("login-password")).sendKeys("Some Password");
        driver.findElement(By.xpath("html/body/main/div[2]/div/div/div[1]/div/div/div/form/button")).click();
    }
}

【问题讨论】:

  • 我取消了两次睡眠,它对我来说效果很好,根本不需要等待。不过我正在使用 Chrome 驱动程序...
  • 你可以开始使用QMetry Automation Framework,它提供了扩展的webdriver和webelement,可以在内部自动处理这种情况。还有明确的等待条件,assertions, verification 可用于元素。例如,element.waitForPresent()element.assertPresent()element.verifyPresent()

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

是的,使用Thread.sleep() 通常是不好的做法。睡眠不是动态的。他们只等待指定的时间……不多也不少。这通常不好,因为如果您正在等待的元素在 25 毫秒内返回,那么您将等待整整 10 秒。如果元素出现在 10.5 秒,那么它将失败。通过自动化,我们希望在保持一致结果的同时尽可能快地进行。使用WebDriverWait 将允许脚本暂停并等待指定的条件。一旦满足条件,脚本将继续。如果不满足,脚本将暂停指定的时间并重试,直到满足条件或发生超时(引发异常)。因此,请确保您等待合理的等待时间。

改为使用WebDriverWait,如下所示。看看ExpectedConditions。您可以等待许多条件...元素的存在、可点击的元素等。

我切换到 geckodriver 并再次尝试了代码。我更改了您的定位器以使其更具体。他们基本上是在寻找包含特定文本的标签。您可以多次重复使用该定位器。下面的代码对我有用。我删除了 sleep 并将它们替换为 WebDriverWait

driver.get("http://qtpselenium.com/");
driver.findElement(By.xpath("//button[contains(.,'Member Login')]")).click();
WebDriverWait wait = new WebDriverWait(driver, 5); // create a WebDriverWait that we will reuse
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(.,'Login to Selenium Account')]"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email"))).sendKeys("Some Email ID");
driver.findElement(By.id("login-password")).sendKeys("Some Password");
driver.findElement(By.xpath("//button[contains(.,'Login')]")).click();

【讨论】:

    【解决方案2】:

    也许试试这个而不是 thread.sleep():

    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2016-12-18
      • 2012-11-27
      • 1970-01-01
      • 2016-10-24
      • 2013-10-09
      • 1970-01-01
      相关资源
      最近更新 更多