2017-05-16 48 views
1

我有以下HTML中我嘗試使用紅寶石硒網絡驅動程序測試紅寶石硒網絡驅動器:找到的XPath元素不拋出NoSuchElementException異常

<div class="container-fluid container-results"> 
    <div class="result-row" id="0"> 
     <ul> 
      <li class="process-status-column upload-success-icon"> 
       <svg class="glyphicon-upload-green" viewBox="0 0 100 100"> 
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#doc_upload"></use> 
       </svg> 
      </li> 
      <li class="file-label-column"><span>validated.txt</span></li> 
      <li class="client-company-column"><span>test-client</span></li> 
      <li class="reference-column"><span>Ref001</span></li> 
     </ul> 
    </div> 
    <div class="result-row" id="1"> 
     <ul> 
      <li class="process-status-column upload-error-icon"> 
       <svg class="glyphicon-exclamation" viewBox="0 0 100 100"> 
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#exclamation"></use> 
       </svg> 
      </li> 
      <li class="file-label-column"><span>Error.txt</span> 
       <div class="attachment-row">This invoice contains an error</div> 
      </li> 
      <li class="client-company-column"><span>test-client</span></li> 
      <li class="reference-column"><span>Ref002</span></li>    
     </ul> 
    </div> 
    <div class="result-row" id="2"> 
     <ul> 
      <li class="process-status-column upload-pending-icon"> 
       <svg class="glyphicon-spin" viewBox="0 0 100 100"> 
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#refresh"></use> 
       </svg> 
      </li> 
      <li class="file-label-column"><span>Pending.txt</span></li> 
      <li class="client-company-column"></li> 
      <li class="reference-column"></li>    
     </ul> 
    </div> 
</div> 

我試圖找到ul元素爲selectedLine變量,它匹配在span值按輸入參數unique_field_value

selectedLine = nil 
rows = driver.find_elements(:css => "div.container-fluid.container-results >div.result-row >ul") 
rows.each do |row| 
    element = row.find_element(:xpath => "./li/span[contains(., '#{unique_field_value.to_s.strip}')]") 
    if(!element.nil?) 
    selectedLine = row 
    end 
end 

上面的代碼工作正常,如果我通過validated.txt作爲參數值unique_field_value但我F I傳遞異常Error.txtPending.txt上午getttig下面

no such element: Unable to locate element: {"method":"xpath","selector":"./li/span[contains(., 'Error.txt')]"} (org.openqa.selenium.NoSuchElementException) 

我的代碼試圖找到span元件匹配在rows的第一次迭代unique_field_value參數和throwning異常,如果它不找一個,我該如何繼續迭代超過rows沒有拋出NoSuchElementException以外的所有元素

+0

你可以看到,如果'元素= row.find_element(:的xpath => '// * [文本()= 「#{unique_field_value.to_s.strip}」]')'匹配一些元件? –

+0

在文體上,我們不使用camelCase來處理Ruby中的變量或方法。 ItIsAReadabilityThing,所以'selectedLine'應該是'selected_line'。 –

回答

0

嘗試:

rows.each do |row| 
    element = row.find_element(:xpath => "./li/span[contains(., '#{unique_field_value.to_s.strip.downcase}')]") 
    if(!element.nil?) 
    selectedLine = row 
end 
0

得到了它下面代碼工作

rows.each do |row| 
    elements = row.find_elements(:xpath => "./li/span[contains(., '#{unique_field_value.to_s.strip}')]") 
    if (elements.length > 0) 
     selectedLine = row 
    end 
    end 
相關問題