java - 为什么 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.
}
解决方案
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
推荐阅读
- git - Git diff —“工作树中没有这样的路径”
- eclipse - 推送到远程时出现 Eclipse git 错误:传输错误:无法获取远程存储库引用
- javascript - 如何根据我所在的页面将按钮标记为活动?
- python - 如何使用 re.sub 删除数字
- javascript - 在 Firebase 中创建用户并同时在 Angular 中将其数据保存在 Firestore 中
- node.js - nodejs和babel:从未定义的结果中导入
- javascript - 无法在 window.print() 上使用 CSS
- java - XML 签名验证失败
- javascript - 如果调用funtionA Insider funtionB,会调用functionA吗?编译级别发生了什么?
- swift - 是否可以在命令行模式下运行仅限 Swift 的 Cocoa 应用程序