Ruby Selenium Web Driver:找到xpath元素而不抛出NoSuchElementException

时间:2017-05-16 20:49:36

标签: ruby selenium xpath selenium-chromedriver

我有一个下面的html尝试使用ruby selenium web驱动程序进行测试

<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传递,但是如果我将Error.txtPending.txt传递给异常,则上述代码可以正常工作

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

我的代码试图在span的第一次迭代中找到匹配unique_field_value元素的rows元素,如果找不到,则如何继续迭代rows的元素没有抛出NoSuchElementException异常

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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