Reputation: 23
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!
Upvotes: 0
Views: 2192
Reputation: 23
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!
Upvotes: 1
Reputation: 4424
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");
Upvotes: 0
Reputation: 2210
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)));
Upvotes: 0