-3

I am writing a selenium code.

Unless I put the Thread.Sleep in a try catch block it won't work. It actually throws a compile time error.

Why so?

 public void test() {
  
  System.out.println("in the test method");
  achromeDriver.get(abaseUrl);
  
  try {
   Thread.sleep(6000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  WebElement benzRadioBtn = achromeDriver.findElement(By.id("benzradio"));
  benzRadioBtn.click();
  
  WebElement benzCheckBox = achromeDriver.findElement(By.id("benzcheck"));
  benzCheckBox.click();
  
  System.out.println("Is ben radio button selected ? "+  benzRadioBtn.isSelected());
  
 }
sutterhome1971
  • 380
  • 1
  • 9
  • 22

1 Answers1

3

The Thread.sleep() method throws an InterruptedException. Whether or not this exception will actually get thrown is dependent on what happens during the execution of your java code, the method is just letting you know it could happen, and that you should handle it in some way.

One way to handle the exception is to put it inside a try catch block, so if the exception gets thrown, the program will still continue and the code inside the catch block will execute.

If you really don't want a try catch block (no idea why you wouldn't) you can add a throws declaration at the top of your method which would look something like this:

    public void test() throws InterruptedException {

I would read up some more about java exceptions and how they work

https://stackify.com/specify-handle-exceptions-java/

https://www.geeksforgeeks.org/exceptions-in-java/

BeyondPerception
  • 534
  • 1
  • 6
  • 10