Selenium PHPUnit select element by label text

独自空忆成欢 提交于 2020-02-02 16:12:34

问题


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.


回答1:


//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



来源:https://stackoverflow.com/questions/28678617/selenium-phpunit-select-element-by-label-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!