0

I am new to selenium. And I wanted to select checkboxes in my webpage using selenium webdriver, by getting the data from a local excel sheet. I achieved this for all the radio buttons, but these checkboxes I wanted to select has no 'id' attribute. They are distinguished only by 'value'.

The HTML code is:

    <input type="hidden" value="1" name="%%Surrogate_Competitors"/>
    <label>
    <input type="checkbox" title="Competitors" value="HP (Servers)" name="Competitors"/>
    HP (Servers)
    </label>
    <br/>
    <label>
    <input type="checkbox" title="Competitors" value="Sun (Servers)" name="Competitors"/>
    Sun (Servers)
    </label>
    <br/>
    <label>
    <input type="checkbox" title="Competitors" value="HP (Storage)" name="Competitors"/>
    HP (Storage)
    </label>
    <br/>
    ............
    ............

I tried using this code to select those checkboxes:

String Competitors=sh.getCell(column,1).getContents();
String delims = "[,]";
String[] Competitor = Competitors.split(delims);

for(int i=0;i<Competitor.length;i++) {
    //to select competitors
    driver.findElement(By.cssSelector("input[value="+Competitor[i]+"]")).click();
}

when I run this code, an error message is getting generated as below:

The given selector input[value=HP (Storage)] is either invalid or does not result in a WebElement.

Please let me know how to overcome this, and get the check boxes selected.

1 Answers1

0

please check the css identifier, you may need to add quote for the value:

 driver.findElement(By.cssSelector("input[value='" + Competitor[i] + "']"))

This stack post must be helpful

Community
  • 1
  • 1
Vignesh Paramasivam
  • 2,360
  • 5
  • 26
  • 57