Bounce
Bounce

Reputation: 2095

Selenium PHPUnit select element by label text

I have the following HTML code snippet:

<div class="modal-body" style="max-height: 317px;">
   <div class="radio">
      <label>
         <input type="radio" name="template" value="5">
         <img src="/img/custom-icons/Monitor.png" style="height: 24px">
         First descriptive title                    
         <p class="text-muted">Some description text</p>
      </label>
   </div>
   <div class="radio">
      <label>
         <input type="radio" name="template" value="37">
         <img src="/img/custom-icons/Monitor.png" style="height: 24px">
         Second descriptive title                   
         <p class="text-muted"></p>
      </label>
   </div>
   <div class="radio">
      <label>
         <input type="radio" name="template" value="46">
         <img src="/img/custom-icons/Chart_Area_Up.png" style="height: 24px">
         Third descriptive title                    
         <p class="text-muted">Some text that describes an item.</p>
      </label>
   </div>
</div>

I want to select radio element by its label text and click it. What I'm trying to do:

$this->byXPath( "//label[text()='Second descriptive title']" )->click();

Unfortunately that is not working. Any ideas? I would be appreciated for your help.

Upvotes: 0

Views: 1201

Answers (1)

Saifur
Saifur

Reputation: 16201

//div[@class='radio']//*[contains(.,'Second descriptive title ')]//input[@type='radio']

Pretty tricky. There could be a better way. But, this is what I can suggest. Doing a tag independent search on self with //*[contains(.,'Second descriptive title')] and after that just walking to the input radio tag

Upvotes: 2

Related Questions