1

I am new to selenium webdriver. I'm writing script using java but I got stuck in writing script, I want to select a radio button but I'm getting

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not displayed (WARNING: The server did not provide any stacktrace information) error.

This is I have written:

WebElement radioButton = driver.findElement(By.id("Radio_0_2461A"));
radioButton.sendKeys(Keys.SPACE);

Below is HTML code:

HTML code for Find button.

<button class="button right secondary" id="selectProducts0" onclick="getProducts('0'); return false;">
        Find Products
    </button>

Once button clicked product will load and I have to select product from list.

    <div class="clearboth" id="productList0" style="overflow: hidden; display: block;">
    <div class="productTableContainer" data-index="0">

 <table class="responsive" cellspacing="0" cellpadding="0" summary="List of products">
   <tbody>            
    <tr data-row-data='{"earlyRepaymentCharges":[],"incentives":[]}'>
    <th class="radio">
    <input id="0_2689A_StartDate" type="hidden" value="28/06/2016 00:00:01">
    <input name="Products[0].ProductCode" title="Lifetime Tracker with £999 Fee" id="Radio_0_2689A" type="radio" value="2689A"> <label for="Radio_0_2689A">
     Lifetime Tracker with £999 Fee </label>

<tr data-row-data='{"earlyRepaymentCharges":[{"step":"1","durationInMonths":"","endDate":"30/11/2016","percentage":"1%"}],"incentives":[]}'>
    <th class="radio">
   <input id="0_5555A_StartDate" type="hidden" value="01/11/2015 00:01:00">
   <input name="Products[0].ProductCode" title="1 Year Fixed Rate Until 30/11/2016 with £999 Fee" id="Radio_0_5555A" type="radio" value="5555A">
 <label for="Radio_0_5555A"> 1 Year Fixed Rate Until 30/11/2016 with £999 Fee
 </label>

<tr data-row-data='{"earlyRepaymentCharges":[{"step":"1","durationInMonths":"","endDate":"28/02/2017","percentage":"2%"},{"step":"2","durationInMonths":"","endDate":"28/02/2018","percentage":"1%"}],"incentives":[]}'>
 <th class="radio">
<input id="0_2461A_StartDate" type="hidden" value="18/12/2015 00:01:00">                          
<input name="Products[0].ProductCode" title="2 Year Fixed Rate Until 28/02/2018 with £999 Fee" id="Radio_0_2461A" type="radio" value="2461A">
 <label for="Radio_0_2461A">2 Year Fixed Rate Until 28/02/2018 with £999 Fee
</label>
1
  • Please post your code in the question itself rather than link us to it. Commented Aug 1, 2016 at 16:31

1 Answer 1

1

I think radio button I'd is dynamically generated. Try using By.name() with WebDriverWait to wait until radio button visible as below :-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Products[0].ProductCode")));
radio.click();

Edited1 :- If you radio button id is fixed then try using By.id with WebDriverWait to wait until radio button visible as below :-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Radio_0_2461A")));
radio.click();

Edited2:- If you can find radio button but not selecting due to visibility, you can try using JavascriptExecutor to select the radio as below :-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Radio_0_2461A")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", radio);

Edited3:- if unfortunately javascript click does not work on radio button try using Javascript Mouse event to perform click as below :-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Radio_0_2461A")));
((JavascriptExecutor)driver).executeScript("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent ('click', true, true);arguments[0].dispatchEvent (clickEvent);", radio);

Hope it helps...:)

Sign up to request clarification or add additional context in comments.

12 Comments

@PrashantBellale and what about I'd?? It's dynamically changed or fixed??
I think ID is fixed, I tried with ID but still it is not selection radio button. actual scenario is I have click on find product button then wait till product load, once product loaded then select product. I'll answer with all code, it will help to understand.
@PrashantBellale try By.id("Radio_0_2461A"). if id is fixed using WebDriverWait to wait until it's visible and let me know the result..:)
I'm geting Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for visibility of element located by By.id: Radio_0_2461A. I tried with 10 second and then I changed to 30 second.
Thanks Saurabh. It is working now. I have used By.id("0_2461A_StartDate")`.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.