java - How to select radio button in selenium webdriver? -


i new selenium webdriver. i'm writing script using java got stuck in writing script, want select radio button i'm getting

exception in thread "main" org.openqa.selenium.elementnotvisibleexception: element not displayed (warning: server did not provide stacktrace information) error.

this have written:

webelement radiobutton = driver.findelement(by.id("radio_0_2461a")); radiobutton.sendkeys(keys.space); 

below html code:

html code find button.  <button class="button right secondary" id="selectproducts0" onclick="getproducts('0'); return false;">         find products     </button> 

once button clicked product load , have select product 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 £999 fee" id="radio_0_2689a" type="radio" value="2689a"> <label for="radio_0_2689a">      lifetime tracker £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 £999 fee" id="radio_0_5555a" type="radio" value="5555a">  <label for="radio_0_5555a"> 1 year fixed rate until 30/11/2016 £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 £999 fee" id="radio_0_2461a" type="radio" value="2461a">  <label for="radio_0_2461a">2 year fixed rate until 28/02/2018 £999 fee </label> 

i think radio button i'd dynamically generated. try using by.name() webdriverwait wait until radio button visible below :-

webdriverwait wait = new webdriverwait(driver,10); webelement radio = wait.until(expectedconditions.visibilityofelementlocated(by.name("products[0].productcode"))); radio.click(); 

edited1 :- if radio button id fixed try using by.id webdriverwait wait until radio button visible below :-

webdriverwait wait = new webdriverwait(driver,10); webelement radio = wait.until(expectedconditions.visibilityofelementlocated(by.id("radio_0_2461a"))); radio.click(); 

edited2:- if can find radio button not selecting due visibility, can try using javascriptexecutor select radio 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 not work on radio button try using javascript mouse event perform click 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 helps...:)


Comments