0

I am building a test suite for a basic registration page (it has a large number of text fields + some radio buttons and checkboxes here and there). I am using Selenium Webdriver solution and tests are written in Java. While the tests are running fine on Firefox and Chrome, the Internet Explorer tends to run into trouble when it comes to clicking on radio buttons or checkboxes. All the radio buttons and checkboxes have id-s defined and from what I've learned it's the most convenient way to find an element on the page, so I was quite surprised when I started getting these issues. The method for finding the radio button looks like this:

public static WebElement rad_Male(WebDriver driver) {
    element = driver.findElement(By.id("male"));
    return element;
}

The click is done in a following way:

rad_Male(driver).click();

As I said, Firefox and Chrome can easily click on checkboxes and radio buttons, but when running tests in IE I get a following exception (the element is visible all the time and I can click on it with mouse):

org.openqa.selenium.ElementNotVisibleException: Cannot click on element

I've also tried using an explicit wait in order to let the elements load before accessing them, but had no luck - I get TimeoutException as soon as the function times out. I suspect it has something to do with the page design, but unfortunately I have no access to the page source code, so I cannot change the page structure to make it easier to test. The radio button is placed inside a number other divs and I think there is also a table used to align this and other elements around, but this doesn't look too complicated. Here's the code for radio button:


<input type="radio" value="M" name="sex" id="male" tabindex="110">

I think I saw some javascript click suggestion in one of the similar topics, but before resorting to this I wanted to make sure that there is no other way to make it work using the means that Webdriver provides. I've just started learning Selenium and I try to get my work done on the go while learning new stuff all the time, so I am not too experienced with this yet. If you would like some more details, please ask as I am not sure if I've got all included. Thanks in advance!

  • Not sure if it helps, but IE can access dropdown menus and select items from these. – Bob the Dawg Nov 26 '14 at 08:18
  • Have you examined these elements of this page under IE using developer tools ? Just press F12 when you are on this page, it brings a tool window (similar to FireBug on Firefox), and then examine the web structure. Could be that these elements are rendered diferently under IE, and there is no `id` assigned to them, or these `id` are different than on Firefox/Chrome? – krokodilko Nov 26 '14 at 22:00
  • Hi and thanks for answering! I have just noticed that my html code for the radio button in the post above was not visible, so I did some tweaking and you should see it now in the original post. I checked the code both on IE and Firefox and the only difference between the two browsers is the order for the properties (here's what it looks like in IE): – Bob the Dawg Nov 27 '14 at 08:45
  • Please also post a html code of a parent of this element (an outer html) - maybe even a grandparent (2 or 3 levels up). Is there any javascript code in any of it's parents that is fired on some events (for example onmouseover etc)? – krokodilko Nov 27 '14 at 19:04

3 Answers3

1

Hi again and thanks to everyone who responded! A friend of mine had a look at this issue and managed to figure out what was causing this. The radio button was actually contained inside another div in a following way:

<div class="radio" id="uniform-male">
    <span>
        <input type="radio" value="M" name="sex" id="male" tabindex="110">
    </span>
</div>

It appears that this parent div "uniform-male" kind of concealed this button, because Selenium was able to click on this div and as a result, the radio button underneath it was clicked. I guess I should have posted the code for the radio button along with some code of it's parent elements in the first hand, so it would have been easier to debug it.

Once again I appreciate all the help I received from you on this question, thanks!

  • I'm also having this problem after updating my IEDriverServer to 2.45. Do you mind me asking what selector did you use? – user1816183 Apr 14 '15 at 15:45
  • I don't have my code at hand right now, but I'm pretty sure I called findElement(By.id("uniform-male")) and performed a click on it. I hope it'll work for you... – Bob the Dawg Apr 17 '15 at 18:28
  • That does work for me however how do you get the state of the checkbox (i.e. determine whether it is checked or not?) – user1816183 Apr 20 '15 at 08:50
  • In the current scenario I am not doing any state checks, but I think the following discussion might help you: http://stackoverflow.com/questions/8187772/selenium-checkbox-attribute-checked – Bob the Dawg Apr 21 '15 at 18:31
  • Thanks....that link works fine for checkboxes that aren't wrapped in divs...the problem arises when the checkbox is wrapped in a div. As the selector you are using to click on the checkbox is not actually the checkbox itself (it's the wrapping div), it won't be possible to get the checkboxes state using the same selector – user1816183 Apr 22 '15 at 07:57
  • Yeah, now I get it. It really seems like a dead end to me, I've got no ideas for helping you out, sorry. – Bob the Dawg Apr 23 '15 at 21:23
0

try using this before you click on the element, maybe IE is a bit slower:

 WebDriverWait wait = new WebDriverWait(driver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(anId)));
arisalexis
  • 2,079
  • 2
  • 21
  • 42
  • I tried your solution - it resulted in a wait timeout. There is another interesting detail - I got the same result in Firefox and Chrome too. This means that these elements never become "visible" in such way that it could be detected by wait.until conditions (although one can clearly see them on the page). Still, somehow Firefox and Chrome manage to retrieve them and IE fails to. I think we can narrow this topic to how could such "hidden" elements be retrieved in IE... Thank you for contributing though! – Bob the Dawg Nov 27 '14 at 11:32
  • you can also try elementToBeClickable instead of visibilityOfElement – arisalexis Nov 27 '14 at 12:06
  • Sorry, already tried that and it timed out just the same way. – Bob the Dawg Nov 27 '14 at 15:53
0

As per the error you are getting, I think selenium is trying to click on the element, i.e., radio button, which is probably not visible yet.

To resolve this, try adding an explicit wait in the method rad_Male like this (Assuming 'element' is a reference of 'WebElement class'):

    public static WebElement rad_Male(WebDriver driver) {
        //waiting 30 seconds for the element to be visible
        element = new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.id("male")));
        return element;
    }

Then, use it to click the button like this:

    element = rad_Male(driver); //Fetching the value returned by rad_Male method
    if(element!= null)
        element.click();
    else
        System.out.println("Element is not visible");
Subh
  • 4,354
  • 1
  • 13
  • 32
  • Hi and thanks for contributing! I have already tried using explicit wait as was suggested in the previous comment by arisalexis. This results in wait function timeout. However, I like the check you use to see, if element exists, I think I will add this to my code so I can set test value to "Fail" if Selenium fails to retrieve the element. – Bob the Dawg Nov 28 '14 at 08:46