0

I'm using Selenium WebDriver, and want to tick a specific HTML checkbox based on the value of a column called MovieType in an Excel file. The checkbox should be ticked if the column contains Yes and unticked if it contains No.

The HTML code looks like this:

<input type="checkbox" onclick="trigger functionA();" tabindex="4" value="vnow" name="movie_Type"/>

How can I set the value of the checkbox if my Excel file contains Yes/No rather than vnow? The HTML is written by another developer, so I'm not allowed to amend it.

Here's my Java code for Selenium:

public static WebElement chkbx_selectMovieType(WebDriver driver, String value) throws Exception{
    try{            
        List<WebElement> chkbxMovieType = driver.findElements(By.name("movie_Type"));
        for(WebElement chkbx : chkbxMovieType){
            if(chkbx.getAttribute("value").equalsIgnoreCase(value))
                chkbx.click();
        }
    }catch (Exception e){
        throw(e);
    }
    return element;
}

Any suggestion is welcome. Thanks.

GoBusto
  • 4,632
  • 6
  • 28
  • 45
christo
  • 93
  • 2
  • 4
  • 11
  • Just so that we're clear: Do you want Selenium to open a local Excel file (and extract the contents) if a certain check-box on the web page is ticked? If so, it sounds like you're actually dealing with two different things here: (1) How to read an Excel file using Java (2) How to interact with a web page using Selenium. It seems that you already have the second part working, so perhaps you just need to solve the *first* part? **EDIT:** Perhaps [this](http://stackoverflow.com/questions/6202672) or [this](http://stackoverflow.com/questions/14190556) will help you. – GoBusto Jan 27 '15 at 08:50
  • possible duplicate of [How to read excel data in java, selenium testing?](http://stackoverflow.com/questions/14190556/how-to-read-excel-data-in-java-selenium-testing) – GoBusto Jan 27 '15 at 08:54
  • Hi @GoBusto, I know how to do (1) & (2) as well, since the checkbox value in html is not neither "y, n, true, or false", how can I make it into "y, n, true, or false" so in Excel file user is key in a readable value (e.g true), then selenium will checked the checkbox if excel column for value MovieType is true – christo Jan 27 '15 at 08:58
  • OK, I see - so the problem is that the HTML file defines the checkbox as ``, but you want to see Yes/No rather than "vnow" when you query the value. I'm not sure, but I don't think that this can be worked around unless the HTML file is changed. **EDIT:** Perhaps [this](http://stackoverflow.com/questions/20201838/determining-if-a-checkbox-is-checked-in-html-and-php) will help - it says to use `checked` instead of `value`. – GoBusto Jan 27 '15 at 09:03
  • previously selenium can checked the checkbox (if the value in html is y /true). Now my html value is 'vnow', I don't wan set vnow in excel file so the checkbox will checked. I hope somethings readable like y or true – christo Jan 27 '15 at 09:05
  • @GoBusto, you are right. Any suggestion solution since I'm not able to touch the html code – christo Jan 27 '15 at 09:06
  • Can u try out with JavascriptExecutor to change the value as `document.getElementsByName('movie_Type')[0].value='yes'`. But this would take a comparable time for execution rather than just getting getAttribute('value').contains('vnow') chkbox.click(); – Vivek Singh Jan 27 '15 at 09:09

2 Answers2

1

To know if a checkbox element's state, use the .isSelected() method. That will return a boolean value, and that can be the condition as to whether to access your Excel document or not.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
0

It seems that the solution would be to use the checked property rather than the value attribute, as described here.

checkbox: A check box. You must use the value attribute to define the value submitted by this item. Use the checked attribute to indicate whether this item is selected. You can also use the indeterminate attribute to indicate that the checkbox is in an indeterminate state (on most platforms, this draws a horizontal line across the checkbox).

For example, the following Javascript adds a new checkbox to the page and logs the checked-ness to the browser console:

"use strict";

var x = document.body.appendChild(document.createElement("input"));
x.setAttribute("type", "checkbox");

console.log(x.checked);

More information can be found here.

So, it seems that you'll simply need to change this line in your Java code:

if(chkbx.getAttribute("value").equalsIgnoreCase(value))

EDIT: See this answer for more information about getting the checked-ness of a checkbox using the Selenium Java API.

Community
  • 1
  • 1
GoBusto
  • 4,632
  • 6
  • 28
  • 45
  • `if(chkbx.getAttribute("checked").equalsIgnoreCase(value))` then in excel value I should put what to indicate for check or unchecked action? – christo Jan 27 '15 at 09:23
  • You would think so, wouldn't you? But not quite - as per one of the comments on [this question](http://stackoverflow.com/questions/901712), it seems that `checked` is a *property* rather than an *attribute*, so `getAttribute()` won't work. This is why my Javascript example uses `x.checked` rather than `x.getAttribute("checked")` when logging the checked-ness to the browser console. – GoBusto Jan 27 '15 at 09:30
  • I think you misunderstood my question. I want to set `y` or `n` in excel, not html. if excel data is 'y', checkbox will be checked. – christo Jan 29 '15 at 06:54
  • I think that you need to clarify the question then, because I'm honestly not sure exactly what it is that you're trying to do. – GoBusto Jan 29 '15 at 14:23
  • sorry for my poor explanation. In order to Parameterization of Selenium Tests with Microsoft Excel, at `MovieType` column in Excel, if user set `y` then MovieType checkbox will `checked`. if user set `n` then MovieType checkbox will `unchecked`. The problem is the HTML file defines the checkbox as `` I don't want set it as `vnow` in Excel, I want it set as `y` or `n` – christo Feb 04 '15 at 06:57
  • OK, I've edited your question so that it should now (hopefully) reflect what it is that you're trying to do - please let me know if I'm still misunderstanding in some way. I think that the general concept of this answer still applies, though - you want to set the `checked` property rather than use the `value` attribute. – GoBusto Feb 04 '15 at 08:50