簡體   English   中英

watir-webdriver:如何從我在其中找到子字符串的HTML中檢索整行?

[英]watir-webdriver: how to retrieve entire line from HTML for which I found substring in it?

我從服務器發出的HTML中有類似的內容:

<html ...>
<head ...>
....
<link href="http://mydomain.com/Digital_Cameras--~all" rel="canonical" />

<link href="http://mydomain.com/Digital_Cameras--~all/sec_~product_list/sb_~1/pp_~2" rel="next" />
...
</head>
<body>
...
</body>
</html>

如果b保持瀏覽器對象導航到我需要瀏覽的頁面,我是否可以使用b.html.include?找到rel="canonical" b.html.include? 語句,但是如何檢索找到此子字符串的整行? 我還需要下一個(不是空的)。

您可以使用css-locator(或xpath)來獲取鏈接元素。

以下將返回rel屬性值為“ canonical”的link元素的html(將是該行):

b.element(:css => 'link[rel="canonical"]').html
#=> <link href="http://mydomain.com/Digital_Cameras--~all" rel="canonical" />

我不確定您的意思是“我還需要下一個(不是空的)”。 如果您想要的是rel屬性值為“ next”的那個,則可以類似地執行以下操作:

b.element(:css => 'link[rel="next"]').html
#=> <link href="http://mydomain.com/Digital_Cameras--~all/sec_~product_list/sb_~1/pp_~2" rel="next" />

您可以使用String#each_line b.html遍歷b.html每一行並檢查rel=

b.goto('http://www.iana.org/domains/special')
b.html.each_line {|line| puts line if line.include? "rel="}

那應該返回所有包含rel=字符串(盡管它可以返回不需要的行,例如帶有rel屬性的<a>標記)。

另外,您可以使用nokogiri解析HTML:

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open("http://www.iana.org/domains/special"))
nodes = doc.css('link')
nodes.each { |node| puts node}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM