if/else condition is not working as expected



  • WebElement radio= driver.findElement(By.xpath("//[@id=\"mydiv\"]/form/div[1]/div[3]/label"));
    
    if (radio.isSelected()){
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"mydiv\"]/form/div[1]/div[2]/label")).click();
    } else { 
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"mydiv\"]/form/div[1]/div[3]/label")).click();
    }
    

    This is code I am using but it is always selecting the else statement even when the condition is true.



  • Note : I am giving my suggestion in Answer as I do not have sufficient reputation to be able to comment.

    First do not use thread sleep.

    Second put some implicit or explicit wait between your element finding and if condition statements.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2