0

In below code, not able to click or getText of the radiobutton using xpath & id both.

Please help me to perform above operations.

<section class="col-md-12 mb5">
    <div class="well well-cust">
        <label class="radio-inline pl0">
            <input type="radio" name="project_task" value="A" onclick="Javascript:projtasks();" id="radio1">&nbsp;&nbsp;Projects Tasks</label>
        <label class="radio-inline">
            <input type="radio" name="project_task" value="B" onclick="nonprojtasks(); " id="radio2">&nbsp;&nbsp;Non Projects Tasks</label>
        <label class="radio-inline">
            <input type="radio" name="project_task" value="C" checked="checked" onclick="assignedTasks();" id="radio3">&nbsp;&nbsp;Assigned Tasks</label>
        <label class="radio-inline">
            <input type="radio" name="project_task" value="D" onclick="issueAssigned(); " id="radio4">&nbsp;&nbsp;Issues Assigned</label>
    </div>      
</section>

Tried to click on the element using xpath & id both.

I tried with below code:

//Import data for Radiobutton
String Radiobutton_val = formatter.formatCellValue(sheet.getRow(i).getCell(1));
System.out.println("Radio button 1 = "+driver.findElement(By.xpath("//*[@id=\"radio1\"]")).getText());
System.out.println("Radio button 2 = "+driver.findElement(By.xpath("//*[@id=\"radio2\"]")).getText());

if(driver.findElement(By.id("radio1")).getText().equals(Radiobutton_val)) {
    driver.findElement(By.xpath("//*[@id=\"radio1\"]")).click();
} else if(driver.findElement(By.id("radio2")).getText().equals(Radiobutton_val)){
    driver.findElement(By.xpath("//*[@id=\"radio2\"]")).click();
}

I expect that it should getText of the radiobutton & based on the getText value it should click on the specified radio button

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Firstly check Is that radio button visible at time of you want to click? and show me output of System.out.println("Radio button 1 = "+driver.findElement(By.xpath("//* [@id=\"radio1\"]")).getText()); – vasudev.p Sep 24 '19 at 08:21

2 Answers2

0

I guess your getText call returns not the expected string? You should check the text of the parent label. Try following:

if(driver.findElement(By.xpath("//*[@id='radio1']//..")).getText().equals(Radiobutton_val)) {                
    driver.findElement(By.xpath("//*[@id='radio1']")).click();
} 
AndiCover
  • 1,724
  • 3
  • 17
  • 38
0

To click() on the radio button with the text read from the excel sheet you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategy:

  • Using xpath and contains():

    String Radiobutton_val = formatter.formatCellValue(sheet.getRow(i).getCell(1));
    // String Radiobutton_val ="Non Projects Tasks";
    // String Radiobutton_val ="Assigned Tasks";
    // String Radiobutton_val ="Issues Assigned";
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(., '" + Radiobutton_val + "')]"))).click();
    
  • Using xpath and contains(normalize-space()):

    String Radiobutton_val = formatter.formatCellValue(sheet.getRow(i).getCell(1));
    // String Radiobutton_val ="Non Projects Tasks";
    // String Radiobutton_val ="Assigned Tasks";
    // String Radiobutton_val ="Issues Assigned";
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(normalize-space(), '" + Radiobutton_val + "')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352