简体   繁体   English

如何使用 Thread.sleep(); 在硒 Webdriver 中?

[英]How to use Thread.sleep(); in selenium Webdriver?

I cant use Thread.sleep();我不能使用Thread.sleep();

Exception:例外:

"Unhandled exception type InterruptedException" is thrown in Selenium Webdriver. Selenium Webdriver 中抛出“未处理的异常类型 InterruptedException”。

Please help, thanks in advance.请帮忙,提前致谢。

Since you are using the sleep() function, this may throw InterruptedException .由于您使用的是 sleep() 函数,因此这可能会抛出InterruptedException To prevent this it's handling is mandatory.为了防止这种情况,它的处理是强制性的。

You can use try-catch:您可以使用 try-catch:

try{
Thread.sleep(5000);
}
catch(InterruptedException ie){
}

OR throws:或抛出:

public static void main(String args[]) throws InterruptedException{
Thread.sleep(5000);
}

If you are using a standard IDE to code, it will automatically give you an option to add this when you hover over the error displayed.如果您使用标准 IDE 进行编码,当您将鼠标悬停在显示的错误上时,它会自动为您提供添加此选项的选项。

Add your code in try - catch block like:

try{
--CODE
Thread.Sleep(1000);
}catch(Expression ex){
system.out.print(ex.getmessage());
}

You shouldn't be using Thread.sleep() in your Selenium tests at all.您根本不应该在 Selenium 测试中使用Thread.sleep() Thread.sleep() makes current thread to cease execution; Thread.sleep()使当前线程停止执行; this could lead to problems when running tests in parallel.这可能会导致并行运行测试时出现问题。

As alternative, you can use pause method of Actions class.作为替代方案,您可以使用Actions类的pause方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM