Watir 和previous_sibling 方法嗯 [英] Watir and the preceding_sibling method hmmm

查看:34
本文介绍了Watir 和previous_sibling 方法嗯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 #test需要 'watir'url_file =file:///home/alain/yo.html"# 和你一样:哟 ='<!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="时间">time1</div><span class="Locus">locus1</span><span class="Locus">locus2</span><body text="andale"><div class="alpha"><div class="时间">time2</div><span class="Locus">locus3</span><div class="时间">time3</div>

<span class="Locus_xxxx">locus4</span><span class="Locus">locus5</span><span class="Locus">locus6</span></html>'浏览器 = Watir::Browser.newbrowser.goto url_fileresult = browser.spans(class: 'Locus_xxxx').map do |sp|时间 = sp.preceding_sibling(tag_name: 'body').text轨迹 = sp.text#{time} #{locus}"结尾结果

这里是答案:... 30 秒后超时,等待 #"Locus_xxxx", :tag_name=>"span", :index=>0} --> {:tag_name=>"body", :adjacent=>:preceding, :index=>0}> 被定位 (Watir::Exception::UnknownObjectException)

注意Justin Ko有previous_sibling和map方法的想法!!普通的服务员要友好:)这里的想法是从 body 标签中获取文本:andale".这来自带有 class= Locus_xxxx

的 span 标记

解决方案

#test需要 'watir'url_file ="file:///yo.html"# 和你一样:哟 ='<!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="时间">time1</div><span class="Locus_1">locus1</span><span class="Locus">locus2</span><span text="andale"><div class="alpha"><div class="时间">time2</div><span class="Locus">locus3</span><div class="Time_yyyy">time3</div>

</span><span class="Locus_xxxx">locus4</span><span class="Locus">locus5</span><span class="Locus">locus6</span></html>'浏览器 = Watir::Browser.newbrowser.goto url_fileresult = browser.divs(class: 'Time_yyyy').map do |dv|轨迹 = dv.parent.parent.preceding_sibling(tag_name: 'span', class: 'Locus_1').text时间 = dv.text#{time} #{locus}"结尾

这有效!结果是[时间3轨迹1"][4.0s完成]

与此相关的主题:Watir 抓取顺序元素:如此简单,但没有>

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

解决方案

#test
require 'watir'


url_file =
"file:///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_1">locus1</span>
<span class="Locus">locus2</span>
<span text="andale">
  <div class="alpha">
    <div class="Time">time2</div>
    <span class="Locus">locus3</span>
    <div class="Time_yyyy">time3</div>
  </div>
</span>
<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.divs(class: 'Time_yyyy').map do |dv|
  locus = dv.parent.parent.preceding_sibling(tag_name: 'span', class: 'Locus_1').text
  time = dv.text
  "#{time} #{locus}"
end

This works ! result is ["time3 locus1"] [Finished in 4.0s]

Topic related to this: Watir scraping sequential elements : so simple, but no

这篇关于Watir 和previous_sibling 方法嗯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆