我有一个下面的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.txt
或Pending.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
异常
答案 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