【问题标题】:Alternate for thread.sleep(9000) in every line in selenium在 selenium 的每一行中替换 thread.sleep(9000)
【发布时间】:2016-09-22 20:08:31
【问题描述】:
I have been using thread.sleep(9000) almost after every line of code in selenium which is making me wait for long.Can anybody suggest me an alternate way to reduce this.As my application is taking time load a page it needs to wait until a particular page is loaded to perform any action.

WebElement un = driver.findElement(By.id("login_username_id"));
            un.sendKeys(username);
            WebElement pwd = driver.findElement(By.id("login_password_id"));
            pwd.sendKeys(password);

            try {
                Thread.sleep(25000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            driver.findElement(By.id("login_submit_id")).click();
            try {
                Thread.sleep(9000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

我想在每一行之后减少 thread.sleep 的使用,并使用一个通用函数,以便它在需要时等待。

【问题讨论】:

    标签: java eclipse selenium-webdriver automation


    【解决方案1】:

    您好,请在您的脚本中使用通用等待,即隐式等待

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

    上面的行告诉 selenium 在抛出任何错误之前为每个 web 元素等待最多 10 秒(注意你可以增加或减少秒数,这取决于你)

    显式等待:当您想要等待特定的 web 元素时(当您认为特定元素的加载时间比平时要长时使用)

    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(ExpectedConditions.visibilityOfElementLocated(your element));
    

    【讨论】:

    • 我使用了这个语句,但是如果我想在每个语句之后等待呢?我应该如何使用它?假设我的元素是 so(By.id("login_password_id"));
    • 在这种情况下,在创建浏览器时使用通用等待,例如 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    • 但是 driver.manage 没有帮助,,,它希望在每一行之后都有 thread.sleep
    • 如果有帮助,请将 driver.manage 的时间增加到 50 秒看看
    • 不,它没有帮助,,,我需要写一些共同的东西来避免这种情况。我收到这个错误。线程“主”org.openqa.selenium.WebDriverException 中的异常:元素在点(858、409.933349609375)不可点击。其他元素会收到点击:
      命令持续时间或超时:147 毫秒构建信息:版本:'2.52.0',修订:'4c2593c',时间:'2016-02-11 19:06:42'系统信息:主机:'desktop-svc71',ip:'10.2.2.54',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8 .0_73'
    【解决方案2】:

    使用下面的例子:

    public class Main{
    
    static WebDriver driver = new FirefoxDriver();
    
    public static void main(String [] args) throws InterruptedException{
    
        WebElement un = driver.findElement(By.id("login_username_id"));
        un.sendKeys(username);
        WebElement pwd = driver.findElement(By.id("login_password_id"));
        pwd.sendKeys(password);
    
        waitForElement(By.id("ur id"),60);
    
        driver.findElement(By.id("login_submit_id")).click();
    
        waitForElement(By.id("ur id"),60);
    
    
    }
    
    /**
     * wait until expected element is visible
     *
     * @param   expectedElement     element to be expected
     * @param   timeout             Maximum timeout time
     */
    public static void waitForElement(By expectedElement, int timeout) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, timeout);
            wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));
    
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.println("print ur message here");
        }
    }
    }
    

    如果你有任何困惑,请告诉我。

    【讨论】:

    • 我应该在哪里提到元素,我的意思是哪一行,你能给我举个例子吗,是这样的吗?说如果我想在这个声明之后等待我应该怎么写? driver.findElement(By.id("login_submit_id")).click();
    • 使用方法如:waitForElement(By.id("login_submit_id") , 60);
    • 你也可以使用 By.cssSelector 或 By.class .... 这将等待最多 60 秒...但是如果在 1 秒后找到元素,等待将结束
    • 就像我有这样的代码..你可以添加你的代码并发送给我 public static void main(String[] args) throws InterruptedException { WebDriver driver=new FirefoxDriver();WebElement un = driver .findElement(By.id("login_username_id")); un.sendKeys(用户名); WebElement pwd = driver.findElement(By.id("login_password_id")); pwd.sendKeys(password);/*try { Thread.sleep(25000); } catch (InterruptedException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); }*/ driver.findElement(By.id("login_submit_id")).click();
    • 我在我更新的代码中给出了一个完整的类示例。复制并导入必要的包并运行它。
    【解决方案3】:

    您可以声明“是否显示特定元素”,以便网络驱动程序将花时间搜索该元素,这将导致执行延迟。

    例如:一个页面可能包含一些按钮,使其成为目标并告诉网络驱动程序找到它。

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2019-11-12
      • 2016-01-13
      • 2015-07-15
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 2012-04-08
      相关资源
      最近更新 更多