Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select radio button using Selenium with value that changes

I need to select the radio button below with value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW"

HTML:

<form name="ViewQueryForm" method="post" action="/equery/getAttachments.do">

<div class="txt_align_left innerdvcont" id="tabmenu1" style="display:">

<div class="clear"></div>

<div class="txt_align_left innerdvcont" id="tabmenu011" name="tabmenu011">

        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead>
             <tr>
            <th width="10%" style="text-align: left"></th>
                    <th width="60%" style="text-align: left">Attachment </th>
                    <th width="30%" style="text-align: left">Date </th>
                    
            </tr>
                </thead>
        
                <tbody>
                <tr>
                <td align="center" valign="top">
                <input type="radio" name="getAttachmentValue" id="getAttachmentValue" value="Dispute_1466718.xlsx;SYPFJPC2MQHC-5-687433#QHBTW"> </td>
                    <td style="padding:5px 4px">Dispute_1466718.xlsx</td>
                    <td style="padding:5px 4px">2021-02-16T10:34:08.617</td>
                    
                        </tr>
                        
                </tbody><tbody>
                <tr>
                <td align="center" valign="top">
                <input type="radio" name="getAttachmentValue" id="getAttachmentValue" value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW">    </td>
                    <td style="padding:5px 4px">QMBT-0029104.xlsx</td>
                    <td style="padding:5px 4px">2021-03-27T08:08:46.09</td>
                    
                        </tr>
                        
                
            </tbody>    
            </table>    
</div>  

So far I have been able to click on it using the code below:

radiobutton2 = driver.find_element_by_xpath("//input[@value='QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW']");
radiobutton2.click()

However, the value changes every time which means that it's not something that I can use when running the code. Is there any way to select the second radio button by default for example. Alternatively, I will know the QMBT-00000 reference, so is there a way to select the radio button by searching for that text?

I have tried:

radiobutton2 = driver.find_element_by_xpath('//*[contains(text(), "QMBT-0029104") and @id="getAttachmentValue"]');
radiobutton2.click()

However, that gives me an error:

Unable to locate element

like image 741
Posey Avatar asked Apr 15 '26 16:04

Posey


1 Answers

Great first question.

If the radio button you need to select will always be the second option, you can select it by the index (below is in C#, but should be similar for Python):

 // get all elements where id = "getAttachmentValue" 
var radioButtons = driver.FindElements(By.Id("getAttachmentValue"));

 // click second element 
radioButtons[1].Click();

Edit (Python):

from selenium.webdriver.common.by import By

radioButtons = driver.find_elements(By.ID, 'getAttachmentValue')

radioButtons[1].click()

I added your HTML to a simple HTML file and was able to select the second radio button using the above example (using C#):

enter image description here

like image 61
Stemado Avatar answered Apr 17 '26 05:04

Stemado