需要编写没有Thread.sleep的硒代码 [英] Need to write the selenium code without the Thread.sleep

查看:64
本文介绍了需要编写没有Thread.sleep的硒代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written the below code to login to the website "qtpselenium.com".

如果我在两者之间给Thread.sleep使代码执行暂停一段时间,则以下代码可以正常工作. 如果我注释Thread.sleep,则代码无法按预期工作. 我尝试使用硒的隐式和显式等待来使驱动程序等待该元素可见,但是代码仅在使用Thread.sleep时才按预期工作.

The below code works just fine if i give Thread.sleep in between to make the code execution halt for some time. If I comment the Thread.sleep, the code doesn't work as expected. I have tried to use implicit and explicit waits of selenium to make driver to wait for the element to be visible but the code only works as expected when i use the Thread.sleep.

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

Is there any way I can make the below code to work without using the Thraed.Sleep statement.

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

and is it a bad practice to use Thread.sleep statements in the selenium code?

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();
    }
}

推荐答案

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

Yes, it is generally bad practice to use Thread.sleep(). Sleeps aren't dynamic. They only wait for the specified time... no more, no less. This generally is not good because if the element you are waiting for comes back in 25ms, then you will wait the full 10s. If the element comes at 10.5s, then it will fail. With automation we want to go as fast as possible while maintaining consistent results. Using WebDriverWait will allow the script to pause and wait for the specified condition. Once the condition is met, the script continues. If it isn't met, the script will pause for the specified amount of time and retry until either the condition is met or a timeout occurs (throws an exception). So, make sure you wait as long as is reasonable to wait.

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

Instead use WebDriverWait, as I have below. Take a look at ExpectedConditions. There are many conditions you can wait for... presence of an element, element to be clickable, etc.

我切换到了geckodriver,然后再次尝试了该代码.我更改了您的定位器,使其更加具体.他们基本上是在寻找包含某些文本的标签.您可以多次重复使用该定位器.下面的代码为我工作.我删除了睡眠,然后将其替换为WebDriverWait.

I switched to the geckodriver and tried the code again. I changed your locators to make them more specific. They are basically looking for a tag that contains certain text. You can reuse that locator several times. The code below is working for me. I removed the sleeps and replaced them with 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();

这篇关于需要编写没有Thread.sleep的硒代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆