0

Following is my source code to select radio button having JavaScript onclick() method.

<span class="adControls">
<input id="RdbtnPreviousDate" name="date" value="RdbtnPreviousDate" onclick="javascript:setTimeout('__doPostBack(\'RdbtnPreviousDate\',\'\')', 0)" type="radio"/>
<label for="RdbtnPreviousDate">27-Jul-2017</label>
</span>

I tried with all following but none of code run successfully.

1) wbd.findElement(By.cssSelector("input[id=RdbtnPreviousDate]")).click();

2) wbd.findElement(By.cssSelector("input[value=RdbtnPreviousDate]")).click();

3)

wbd.findElement(By.xpath("html/body/form/table[2]/tbody/tr[1]/td[2]/span[1]/input")).click();

4) wbd.findElement(By.xpath("//[class='adControls'][@id='RdbtnPreviousDate']]")).click();

5)

wbd.findElement(By.cssSelector("input[onclick=javascript:setTimeout('__doPostBack(\'RdbtnPreviousDate\',\'\')', 0)]")).click();

6)

WebElement input = wbd.findElement(By.id("RdbtnNextDate"));
new Actions(wbd).moveToElement(input).click().perform();

Some of Errors:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #RdbtnNextDate Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Given css selector expression "input[onclick=javascript:setTimeout('__doPostBack('RdbtnPreviousDate','')', 0)]" is invalid:

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
PurviMK
  • 1
  • 1
  • thanks but select method also not worked. – PurviMK Jul 28 '17 at 07:33
  • Is this inside a frame? if yes you might have to switch to frame before you can find this. – Prashant Shukla Aug 01 '17 at 09:19
  • Yes it is inside form element :
    – PurviMK Aug 04 '17 at 08:01
  • Thanks pls suggest – PurviMK Aug 04 '17 at 08:06
  • The entire code is inside Form tag and login/logout/clicking and opening a new page all has worked without considering a form tag. – PurviMK Aug 04 '17 at 10:03
  • Please paste the source code of whole page then I might be able to help you – Prashant Shukla Aug 04 '17 at 11:45
  • I tried but not possible to post entire code due to number of characters constraints.... – PurviMK Aug 16 '17 at 09:20

1 Answers1

0

try to execute a simple javascript(jQuery) to select the radio button

wbd.executeScript("$('input[id=RdbtnPreviousDate]').click()");
jesuisgenial
  • 685
  • 1
  • 5
  • 15
  • Add Cast to 'wbd', ? – PurviMK Jul 28 '17 at 07:40
  • Showing red line under 'executeScript and when hover it "The method executeScript(String) is undefined for the type WebDriver 1. Quick fix available - Add Cast to 'wbd'". – PurviMK Jul 28 '17 at 07:41
  • What kind of webdriver do you use?? I didn't know some of drivers doesn't support executeScript.. Try with another web driver. – jesuisgenial Jul 28 '17 at 07:52
  • jre1.8.0_91 , selenium-server3.3.1, Mozilla-52.0, Eclipse neon – PurviMK Jul 28 '17 at 08:03
  • https://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-java this looks helpful – jesuisgenial Jul 28 '17 at 08:16
  • i tried with if (wbd instanceof JavascriptExecutor) { ((JavascriptExecutor)wbd).executeScript("$('input[id=RdbtnPreviousDate]').click()"); } else { throw new IllegalStateException("This driver does not support JavaScript!"); } Also tried with JavascriptExecutor js = (JavascriptExecutor) wbd; js.executeScript("$('input[id=RdbtnPreviousDate]').click()"); No error and button didnt get selected. – PurviMK Jul 28 '17 at 09:48
  • js.executeScript("setTimeout('__doPostBack(\'RdbtnPreviousDate\',\'\')', 0)"); is this work? – jesuisgenial Jul 28 '17 at 10:00
  • jesuisgenial, tried but no error and also not radio button get selected. – PurviMK Jul 28 '17 at 10:38