Here is my code:
#test
require 'watir'
url_file =
"file:///home/alain/yo.html"
# same as yo:
yo =
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<div class="Time">time1</div>
<span class="Locus">locus1</span>
<span class="Locus">locus2</span>
<body text="andale">
<div class="alpha">
<div class="Time">time2</div>
<span class="Locus">locus3</span>
<div class="Time">time3</div>
</div>
</body>
<span class="Locus_xxxx">locus4</span>
<span class="Locus">locus5</span>
<span class="Locus">locus6</span>
</html>'
browser = Watir::Browser.new
browser.goto url_file
result = browser.spans(class: 'Locus_xxxx').map do |sp|
time = sp.preceding_sibling(tag_name: 'body').text
locus = sp.text
"#{time} #{locus}"
end
p result
And here the answer :
...timed out after 30 seconds, waiting for #"Locus_xxxx", :tag_name=>"span", :index=>0} --> {:tag_name=>"body", :adjacent=>:preceding, :index=>0}> to be located (Watir::Exception::UnknownObjectException)
Note that Justin Ko had the idea of preceding_sibling and map methods !!
Common watir be friendly :)
The idea here is to get the text from body tag : "andale".
And this from the span tag with class= Locus_xxxx
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…