Stack Exchange
selenium - clicking on radio button generated by javascript
My code is written in Java.
I have a set of radio button that is dynamically generated by javascript.
I want to click on one of them.
I use xpath but it doesn't work. 
It says

Exception in thread "main" org.openqa.selenium.NoSuchElementException:
  Unable to locate element:

I tried both deliveryType method below, after I "Inspect elements" on google chrome.
 //WebElement deliveryType = driver.findElement(By.xpath("//div[contains(@id, 'checkout-shipping-method-load')]//*[contains(@name, 'shipping_method')]"));
    //WebElement deliveryType = driver.findElement(By.cssSelector("[name='shipping_method'][id='s_method_matrixrate_matrixrate_5983']"));
    //deliveryType.click();

Below is the inspected element for that radio button.
 <div id="checkout-shipping-method-load">
  <dl class="sp-methods">
  <dt>Select Shipping Method</dt>
    <dd>
        <ul>
          <li>
            <input name="shipping_method" type="radio" value="matrixrate_matrixrate_5983" id="s_method_matrixrate_matrixrate_5983" class="radio validation-passed">
<label for="s_method_matrixrate_matrixrate_5983">Standard Shipping<span class="price">NT$300</span></label></li>
         <li>
           <input name="shipping_method" type="radio" value="matrixrate_matrixrate_5991" id="s_method_matrixrate_matrixrate_5991" class="radio validation-passed">
<label for="s_method_matrixrate_matrixrate_5991">Express Shipping<span class="price">NT$1,200</span></label></li>
        </ul>
    </dd>
  </dl>
</div>

I have tried to code my script to click on it.
I tried my xpath to follow "inspect element" and "view page source".
Both give me same error as no such element found.
How do I make my selenium script to click on one of the radio button that are dynamically generated by javascript?
My xpath should point to raw javascript coding or the html generated by javascript after window load?
Kindly help me. 

Closed: I have used Implict Wait to wait for my javascript to run and load my html. After loaded, I used List to store all of my radio buttons. From the List, I get it by index. 

Thanks to Girish Sortur & JeffC. 
Below is my code. 
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = driver.findElements(By.cssSelector("input[name='shipping_method']"));
methods.get(0).click();


pls read this: http://stackoverflow.com/help/how-to-ask and edit your answer accordingly, noone can help you without you providing some code that you tried and failed

Please do provide the code that you have tried and failed with. However based on my knowledge i have provided an answer. Hope it helps.

I would pull all the INPUTs corresponding to shipping methods into a collection methods and then click the one that I wanted. Sample code is below.
List<WebElement> methods = driver.findElements(By.cssSelector("input[name='shipping_method']"));
methods.get(0).click(); // clicks the first element, Standard Shipping


Great. If this answer solved your problem, please accept it as the answer. Thanks!

The best way to click on a dynamic element is to wait for it to appear. In Selenium you can wait for an element until it appears using WebDriverWait() method. ExpectedConditions are used in Selenium to check for expectations. Here you can use expect the visibility of the element. Here's a sample code - 
WebElement radioBtn = driver.findElement(By.xpath("//input[@id='s_method_matrixrate_matrixrate_5983']"));
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOf(radioBtn));

Also you can try with FluentWait which is the best option to wait for an element to appear. Here's how to do it - 
new FluentWait<WebElement>(radioBtn).
    withTimeout(50, TimeUnit.SECONDS).
    pollingEvery(2,TimeUnit.SECONDS);

FluentWait waits for an element to appear with the options that we specify. Polling checks for the element's presence at that interval. Provide timeout based on the time element takes to appear in DOM. Hope this helps.

What do you mean by expectedconditions?
How do I code in my expected conditions?
Possible to give an example?

Are you getting any errors? I have updated answer for expected conditions link to selenium. You don't need to code it. Its an inbuilt class of selenium using which you can check for the elements visibility.

erm..I guess the way I locating my element is wrong. I still getting unable to locate element.

How do i point to the first radio button generated by using index of radio button?

Updated answer with your element locator @zero. There are many ways to locate an element. Check this out.

The radio button ID is random generated so I can't use the ID. I would like to use index instead of the ID. Is there a way?

Yes there is a way of adding index. Here's an example xpath driver.findElement(By.xpath(".//ul/descendant::input[@type="radio"][1]. Change the index from 1 to 2 to get the second radio element. Hope this helped.
