为什么Thread.sleep200000在该时间之前没有等待我的脚本?

问题描述

在此步骤中,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.
}

解决方法

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...