2
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.

3

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.

3

I believe you are missing out on the asterisk or the tag details, //* selects all elements in a document, regardless of name, or you could provide the tag name (input, span, table, etc).

Try this code:

WebElement radio= driver.findElement(By.xpath("//*[@id='mydiv']/form/div[1]/div[3]/label"));

or

WebElement radio= driver.findElement(By.xpath("//TagName[@id='mydiv']/form/div[1]/div[3]/label"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.