Use WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.id("radio2")));
senior.click();
After seeing the WebSite it looks like the input
is not the element you want to click rather the label
...
So just change the senior
var to:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='RadioButton']//label[@for='radio2']")));
The best way to automate human actions in the case where the element is not clickable dew to other elements covering them is to use Actions
:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='RadioButton']//label[@for='radio2']")));
Actions actions = new Actions(driver);
actions.moveToElement(senior).click().build().perform();
(using JavascriptExecutor
and executeScript
doesn't realy click... it just invokes the method that can be good but not for testing...)
As a last resort use JavascriptExecutor
:
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", senior);