0
  1. What could be the xpath/Css selector for below HTML tag , My question is How to click on a radio button of Bike.
  2. let's Assume below are the dynamic radio button, we have 100 numbers radio buttons are present and here we can not predict the index number of our radio button

HTML:

<table> 
    <tbody>
        <tr><td class="textAlignCenter" id="clientDocTypeSelection"><input class="marginL10" name="clientRadio" type="radio"></td><td id="clientDocTypeDescription"> Bike </td></tr>
        <tr><td class="textAlignCenter" id="clientDocTypeSelection"><input class="marginL10" name="clientRadio" type="radio"></td><td id="clientDocTypeDescription"> Car </td></tr>
    </tbody> 
</table>

2 Answers 2

1

Find the td with the text you are looking for like this:

tds = driver.find_elements_by_xpath("//td[@class='textAlignCenter']")
for td in tds:
   if td.text == 'Bike':
      radio_input = td.find_element_by_xpath(".//input[@type='radio']")
Sign up to request clarification or add additional context in comments.

2 Comments

look for solution in java
I don't think it will take you a lot of effort to translate the code in Java
-1

To click() on the with text as Bike you can use either of the following based Locator Strategies:

  • Using Java and normalize-space():

    driver.findElement(By.xpath("//td[@id='clientDocTypeDescription' and normalize-space()='Bike']//preceding::td[1]/input")).click();
    
  • Using Java and contains():

    driver.findElement(By.xpath("//td[contains(., 'Bike')]//preceding::td[1]/input")).click();
    

Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using Java and normalize-space():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@id='clientDocTypeDescription' and normalize-space()='Bike']//preceding::td[1]/input"))).click();
    
  • Using Java and contains():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[contains(., 'Bike')]//preceding::td[1]/input"))).click();
    

3 Comments

Thanks for the suggestion , but in my requirement I really I don't know td index number of Bike then how i will select the radio button
@SoumyaRanjanDas The required <input> would be always at index 1. Even removing the index should work seamless.
Thank you so much , you are really helpful ... Appreciating you .. thanks a lot, it's working perfectly

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.