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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35474232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:42:32  来源:igfitidea点击:

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

javaselenium-webdriver

提问by Sundarrajapandian

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.

请帮忙,提前致谢。

回答by Pankaj Kumar Katiyar

Add your code in try - catch block like:

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

回答by Prateek

Since you are using the sleep() function, this may throw InterruptedException. To prevent this it's handling is mandatory.

由于您使用的是 sleep() 函数,因此这可能会抛出InterruptedException。为了防止这种情况,它的处理是强制性的。

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 进行编码,当您将鼠标悬停在显示的错误上时,它会自动为您提供添加此选项的选项。