首页 > 解决方案 > 为什么 Thread.sleep(200000) 没有等待我的脚本到那段时间?)

问题描述

Myy 应用程序在该步骤中有点慢,我需要等待所有元素加载 2 分钟,所以我给了 Thread.sleep(200000) 但脚本没有等待那么长时间,它给了我 UnreachableBrowserException 并显示错误连接带远程浏览器。它可能已经死了。

我正在使用 Selenium 4 alpha 版本和 ChromeDriver 也是 4 版本。请帮忙。演示代码如下:

@And("user clicks on Go button and waits for the members to display")
public void user_clicks_on_go_andwaits()
{
   memberPage.clickGo();
   Thread.sleep(200000);
   // after this step a list of element displays and I need to select one in the below step
}

@And("user selects {int} member")
public void select_member(int memberId)
{
  memberPage.selectMember(memberId);
  //selectMember method is defined in the page object.
}

标签: javaseleniumselenium-webdriverui-automationthread-sleep

解决方案


Dont use thred in selenium automation (That is not good practice) Please use webdriver wait in selenium 

Thread sleep force your automation program 

create method for wait and use anywhere in your program 

**//Following code is method of wait** 

    private static WebElement waitForElement(By locator,int timeout)
    {
        WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
        return element;
    }
//If you want wait for particular element you can use that wait method which you are written in program

**waitForElement(By.id(""),20);**  
//Here 20 is in milliseconds and you can use any web element to wait with any locators

推荐阅读