基本上下面是我的代码,我尝试验证excel表中的每个元素是否都存在于网页中(数据在网页中以表格形式显示)。如果不存在,则单元格将为红色而不是绿色。此代码需要很长时间才能执行,因为excel表中的每个数据都会尝试读取表中的每个值。有10行和10行,需要超过2小时才能超越
require 'rubyXL'
require 'roo'
workbook = Roo::Spreadsheet.open(file_name)
workbook1 = RubyXL::Parser.parse(file_name)
workbook.default_sheet='Sheet4'
worksheet1 = workbook1['Sheet4']
choose("lookupSearch", :option => "Contract")
sleep 5
for j in workbook.first_row..workbook.last_row
d=j
for k in workbook.first_column..workbook.last_column
if (k==workbook.first_column && workbook.cell(j,k)!='nil')
fill_in "searchInput", :with => workbook.cell(j,k)
find(:xpath, '//*[@id="searchicon"]').click
sleep 5
elsif(workbook.cell(j,k)=='/')
puts" "
elsif(k==workbook.last_column)
puts""
elsif()
elsif(workbook.cell(j,k)=='nil')
k=k+1
findXpath=page.find('th,td', text: workbook.cell(j,k), :match => :prefer_exact).path
print findXpath
splitXpath=(findXpath.split("/"))
count1=splitXpath.count
value=splitXpath.at(count1-1)
if(value=="th[1]")
finalElementXpath= findXpath.sub("th[1]","th[2]")
elsif(value=="th[2]")
finalElementXpath= findXpath.sub("th[2]","th[3]")
elsif(value=="th[3]")
finalElementXpath= findXpath.sub("th[3]","th[4]")
elsif(value=="th[4]")
finalElementXpath= findXpath.sub("th[4]","th[5]")
elsif(value=="th[5]")
finalElementXpath= findXpath.sub("th[5]","th[6]")
elsif(value=="td[1]")
finalElementXpath= findXpath.sub("td[1]","td[2]")
elsif(value=="td[2]")
finalElementXpath= findXpath.sub("td[2]","td[3]")
elsif(value=="td[3]")
finalElementXpath= findXpath.sub("td[3]","td[4]")
elsif(value=="td[4]")
finalElementXpath= findXpath.sub("td[4]","td[5]")
elsif(value=="td[5]")
finalElementXpath= findXpath.sub("td[5]","td[6]")
end
if (workbook.cell(j,k+1) == (find(:xpath, finalElementXpath).native.text))
#worksheet1.sheet_data[j-1][k-1].change_fill ('008000')
#worksheet1.sheet_data[j-1][k].change_fill ('008000')
#worksheet1.sheet_data[j-1][k-1].change_fill ('008000')
#worksheet1.sheet_data[j-1][k].change_fill ('008000')
print "---#{workbook.cell(j,k+1)}--value --- exist--------for #{workbook.cell(j,k)} ------------ \n"
else
print "-----object #{workbook.cell(j,k+1)} -- does not exist---for #{workbook.cell(j,k)} --------- \n"
#worksheet1.sheet_data[j-1][k].change_fill ('ff0000')
#worksheet1.sheet_data[j-1][k-1].change_fill ('ff0000')
end
else
findXpath=page.find('th,td', text: workbook.cell(j,k), :match => :prefer_exact).path
splitXpath=(findXpath.split("/"))
count=splitXpath.count
value=splitXpath.at(count-1)
if(value=="th[1]")
finalElementXpath= findXpath.sub("th[1]","th[2]")
elsif(value=="th[2]")
finalElementXpath= findXpath.sub("th[2]","th[3]")
elsif(value=="th[3]")
finalElementXpath= findXpath.sub("th[3]","th[4]")
elsif(value=="th[4]")
finalElementXpath= findXpath.sub("th[4]","th[5]")
elsif(value=="th[5]")
finalElementXpath= findXpath.sub("th[5]","th[6]")
elsif(value=="td[1]")
finalElementXpath= findXpath.sub("td[1]","td[2]")
elsif(value=="td[2]")
finalElementXpath= findXpath.sub("td[2]","td[3]")
elsif(value=="td[3]")
finalElementXpath= findXpath.sub("td[3]","td[4]")
elsif(value=="td[4]")
finalElementXpath= findXpath.sub("td[4]","td[5]")
elsif(value=="td[5]")
finalElementXpath= findXpath.sub("td[5]","td[6]")
end
if (workbook.cell(j,k+1) == (find(:xpath, finalElementXpath).native.text))
worksheet1.sheet_data[j-1][k-1].change_fill ('008000')
worksheet1.sheet_data[j-1][k].change_fill ('008000')
print "---#{workbook.cell(j,k+1)}--value --- exist--------for #{workbook.cell(j,k)} ------------ \n"
else
worksheet1.sheet_data[j-1][k-1].change_fill ('ff0000')
worksheet1.sheet_data[j-1][k].change_fill ('ff0000')
print "-----object #{workbook.cell(j,k+1)} -- does not exist---for #{workbook.cell(j,k)} --------- \n"
end
end
end
end
workbook1.write(file_name)