【问题标题】:Why Thread.sleep(200000) is not waiting my script up to that amount of time?)为什么 Thread.sleep(200000) 没有等待我的脚本到那段时间?)
【发布时间】:2021-01-04 23:20:46
【问题描述】:

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.
}

【问题讨论】:

  • 首先,Thread.sleep() 需要以毫秒为单位的时间。 2 分钟是 120000 毫秒,但您输入的是 200000(3 分 20 秒)。其次,我认为我们需要查看一些代码才能在这里为您提供帮助。请阅读minimal reproducible example 页面。
  • 是的,我给了更多时间,因为有时申请也需要超过 2 分钟。我无法共享代码,因为它是特定于项目的。
  • 您是否阅读了我链接的minimal reproducible example 页面?如果没有,请完整执行。如果您按照该页面上的步骤进行操作,那么您发布的内容中绝对没有特定于项目的代码。您甚至可以在发布之前解决自己的问题。
  • 我们不关心你的确切代码,你只需要展示demonstrator代码以我们可以的方式展示这个问题运行,然后去“是的,你是对的,这不是人们期望的那样”,因为没有代码,我们只能说“如果你做得对,它工作正常,而且你没有证明你没有做对”。所以没有代码,没有问题我们帮你解决。
  • 你可以使用fluent wait代替thread.sleep。你试过吗?

标签: java selenium selenium-webdriver ui-automation thread-sleep


【解决方案1】:
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

【讨论】:

  • 我也尝试过 webdriver wait ,但页面加载时脚本仍然失败。是不是跟更多的场景步骤有关?
  • Webdriver 等待是在 selenium 中等待的好习惯
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多