0

我有多个具有相同类名且没有 id 的元素,我需要获取具有类名的第一个元素。所以让我们说html看起来像这样:

<table class="default-table">       
  <thead>
    <tr>    
      <th class="cell"><span class="text"> Title1</span></th>    
      <th class="cell"><span class="text"> Title2</span></th>
      <th class="cell"><span class="text"> Title3</span></th>    
      <th class="cell"><span class="text"> Title4</span></th>    
    </tr>
  </thead>

  <tbody class="tb">
   <tr>      
     <td class="theInput">
     <input type="text" data-type="string" value="" class="text " data-model-key="item1">    
     </td>

     <td class="theInput">
     <input type="text" data-type="string" value="" class="text " data-model-key="item2">  
     </td>

     <td class="theInput">
     <input type="text" data-type="string" value="" class="text " data-model-key="item3">    
     </td>

     <td class="theInput">
     <input type="text" data-type="string" value="" class="text " data-model-key="item4">    
     </td>
   </tr>       
  </tbody>
</table>

现在,我想用“文本”类设置第一个和第三个输入字段的值,如果我使用看起来像这样的 java,我找到了一个解决方案:

classElement = driver.findElements(By.className("text")); 
classElement.get(1).value("1234");
classElement.get(3).value("5678");

我不确定它是否会起作用,因为我不熟悉 java,但它看起来合乎逻辑,PHP 中有这样的解决方案吗?还是在这种情况下调用类来访问元素是更好的方法?

4

1 回答 1

0

我找到了一个看起来像这样的解决方案:

$items = $this->elements($this->using('css selector')->value('table.default-table input.text'));


$items[0]->value('1234');
$items[2]->value('5678');
于 2013-06-05T06:50:38.303 回答