Robert
Robert

Reputation: 77

Get input radio by inner text

This is part of an HTML page where I need to access the first radio button:

<div>
    <div class="clear"> 
        <div class="relativeinput" style="top:8px;">
            <label class="clear" for="txtTempo">
                <input style="background-color: #E7E7E7;" name="rdoPrazoAcesso" type="radio">Unlimited time
            </label>
        </div>  
    </div>

    <div class="clear"> 
        <div class="relativeinput" style="top:20px; width:50px">
            <label class="clear" for="rdoDias">
                <input style="background-color: #E7E7E7;" name="rdoPrazoAcesso" type="radio">For
            </label>
        </div>  
        <div class="relativeinput" style="top:20px;">
            <input disabled="" class="pie | input-white |" size="1" ;="" style="text-align: center;font-size: 1.5em; position: absolute;" name="txtDias" type="text">
            <label class="clear" for="txtDias" style="padding-left: 70px; position: absolute;">
                days
            </label>                                                
        </div>
    </div>

    <div class="clear">
        <div class="relativeinput" style="top:30px;">
            <label class="clear" for="txtAcesso">
                <input style="background-color: #E7E7E7;" name="rdoPrazoAcesso" type="radio">Just for today
            </label>
        </div>  
    </div>
</div>

How can I get the element <input style="background-color: #E7E7E7;" name="rdoPrazoAcesso" type="radio">Por tempo indeterminado in order to be able to select it? Keep in mind that there's more than one radio with the same name.

I tried to do something like

driver.findElement(By.xpath("//input[text()='Por tempo indeterminado']")).click();

but it didn't work.

Thanks.

Upvotes: 0

Views: 259

Answers (1)

Cosmin
Cosmin

Reputation: 2404

If you want to select it by text, you could use xpath's method text():

driver.FindElement(By.Xpath(".//input[text()='Por tempo indeterminado']"));

Another way, would be to get all the radio buttons and get what you need, again by text:

var radioBtnList = driver.FindElements(By.Name("rdoPrazoAcesso"));
foreach(var radioBtnItem in radioBtnList)
{
    if(radioBtnItem.Text == "Por tempo indeterminado")
    {
        //do your stuff
        break;
    }
}

EDIT: in case you have white spaces before or after the text:

driver.FindElement(By.Xpath(".//input[contains(text(),'Por tempo indeterminado')]"));

Upvotes: 2

Related Questions