1
<input id="radio2" name="radioGroup" type="radio">
<label class="flRight" for="radio2">
  ::before
  "Senior Citizen"
  ::after
</label>
WebElement senior = driver.findElement(By.id("radio2"));
      senior.click();

Now the problem is the code is not able to click the desired element.

ABHISHEK DAS
  • 63
  • 1
  • 8
  • Try using different ways of clicking an element. (Link)https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-x-y-other-elem Second way always works for me. – Pratik Nov 05 '19 at 16:47
  • You might need to post the full HTML, or a link, for the page you are trying to automate. Your locator strategy looks fine, and WebDriverWait does not solve the issue either. The most likely problem here is that your locator is not specific enough, and it is not finding the correct element to click. – CEH Nov 05 '19 at 16:52

4 Answers4

1

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);
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
1

Try with this:

WebElement senior = driver.findElement(By.xpath(".//div[@class='radioBtn']/label[2]"));
    WebElement close = driver.findElement(By.xpath(".//div[@id='nvpush_cross']"));
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOf(close));
    close.click();
    senior.click();

Code didnt worked before because there is popup you need to close it.

Pratik
  • 357
  • 1
  • 9
1

You need to induce WebdriverWait And to click on the radio button use JavaScript Executor since neither webdriver click nor action class is working

WebDriverWait wait = new WebDriverWait(driver, 20); 
WebElement item=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='flRight' and @for='radio2']")));
JavascriptExecutor js= (JavascriptExecutor) driver; 
js.executeScript("arguments[0].click();", item);
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Please try below solution :

  WebDriverWait wait = new WebDriverWait(driver, 10);
  WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='flRight' and @for='radio2']")));

  Actions action=new Actions(driver);
  action.moveToElement(element).click().perform();
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30