我有以下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.txt
或Pending.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
以外的所有元素
你可以看到,如果'元素= row.find_element(:的xpath => '// * [文本()= 「#{unique_field_value.to_s.strip}」]')'匹配一些元件? –
在文體上,我們不使用camelCase來處理Ruby中的變量或方法。 ItIsAReadabilityThing,所以'selectedLine'應該是'selected_line'。 –