如何检查
内的文本?

发布于 2024-10-21 19:06:41 字数 362 浏览 9 评论 0原文

我正在尝试访问位于 DIV 中的一些文本。
我需要检查页面是否包含文本,以便我可以返回 true 或 false。
我使用的代码如下:

cancel = browser.text.include?("Current Cancelled")
if cancel == true
puts "Line item cancelled"
else
puts "****Line item not cancelled****"
end

但每次都返回 false。
这是我正在研究的代码片段:

在此处输入图像描述

I am trying to access some text that is located in a DIV.
I need to check to see if the page holds the text so I can return a true or false.
The code I am using is below:

cancel = browser.text.include?("Current Cancelled")
if cancel == true
puts "Line item cancelled"
else
puts "****Line item not cancelled****"
end

But it returns false every time.
Here is a code snippet of what I am looking into:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萤火眠眠 2024-10-28 19:06:41

我真的建议使用 Nokogiri 来解析内容。

require 'nokogiri'

doc = Nokogiri::HTML('<div><span class="label">Current</span>Cancelled</div>')
doc.at('//div/span[@class="label"]/../text()').text # => "Cancelled"

(doc.at('//div/span[@class="label"]/../text()').text.downcase == 'cancelled') # => true
!!(doc.at('//div/span[@class="label"]/../text()').text.downcase['cancelled']) # => true

像下面两个陈述之一这样的东西会给你一个可用的真/假。

I'd really recommend using Nokogiri to parse the content.

require 'nokogiri'

doc = Nokogiri::HTML('<div><span class="label">Current</span>Cancelled</div>')
doc.at('//div/span[@class="label"]/../text()').text # => "Cancelled"

(doc.at('//div/span[@class="label"]/../text()').text.downcase == 'cancelled') # => true
!!(doc.at('//div/span[@class="label"]/../text()').text.downcase['cancelled']) # => true

Something like one of the two bottom statements will get you a usable true/false.

那一片橙海, 2024-10-28 19:06:41

这不起作用的可能原因是您正在测试的字符串包含换行符和不间断空格。

这可以工作......

if browser.div(:text, /Current.*Cancelled/).exists?
  puts "Line item cancelled"
else
  puts "****Line item not cancelled****"
end

或者

if browser.text =~ /Current.*Cancelled/
  puts "Line item cancelled"
else
  puts "****Line item not cancelled****"
end

等等。

The probable reason this isn't working is because the string you're testing for contains a newline character and a non breaking space.

This could work...

if browser.div(:text, /Current.*Cancelled/).exists?
  puts "Line item cancelled"
else
  puts "****Line item not cancelled****"
end

or

if browser.text =~ /Current.*Cancelled/
  puts "Line item cancelled"
else
  puts "****Line item not cancelled****"
end

etc.

记忆里有你的影子 2024-10-28 19:06:41

Watir 的浏览器对象现在具有 #elements_by_xpath 方法...
请参阅 Watir 的 API

只需精确定位您的 DIV 并询问其#文本方法。与铁皮人建议的非常相似,但不需要 nokogiri。

无论如何,AFIK Watir 在内部使用的目的正是为了定位元素(它是 Watir 安装的依赖项 gem)。

Watir's Browser object has now the #elements_by_xpath method...
See Watir's API

Just pin-point your DIV and ask for its #text method. Pretty much like what the Tin Man suggests but without requiring nokogiri.

AFIK Watir uses internally exactly for the purpose of locating elements (it's a dependency gem that Watir installs) anyway.

夏末的微笑 2024-10-28 19:06:41

我相信文本位于表格内这一事实导致了此问题。

您可能会考虑深入研究表,执行以下操作:

cancel = browser.table(:class, 'basic-table').each { |row|
  test = row.text.include?("Current Cancelled")
  return test if test == true
}

I believe the fact that the text is inside a table is causing this problem.

You might consider drilling into the table doing:

cancel = browser.table(:class, 'basic-table').each { |row|
  test = row.text.include?("Current Cancelled")
  return test if test == true
}
辞慾 2024-10-28 19:06:41

哇。这是有道理的。我想知道我如何
可以把它们分开并让它们
结合起来作为我的支票。

好的,这是一个非常快速的草稿:

div = browser.table(:class, 'basic-table').div(:text, /Cancelled/)

cancel = div.exist? and div.span(:index, 1).text == 'Current'
if cancel
   puts "Line item cancelled"
else
   puts "****Line item not cancelled****"
end

Wow. That makes sense. I wonder how I
could split these up and get them to
combine for my check.

Okay, here's a really quick draft:

div = browser.table(:class, 'basic-table').div(:text, /Cancelled/)

cancel = div.exist? and div.span(:index, 1).text == 'Current'
if cancel
   puts "Line item cancelled"
else
   puts "****Line item not cancelled****"
end
乄_柒ぐ汐 2024-10-28 19:06:41

您还可以结合下面的一些正则表达式方法(主要是来自 Kinofrost 的方法),将其范围缩小到仅查看表中的单个单元格内部。如果“当前”和“已取消”这两个词按顺序出现在页面上的其他位置,并且它们之间有任何内容,那么这应该会更快,并且不太容易出现错误警报。

if browser.table(:class, 'basic-table').cell(:text, /Current.*Cancelled/).exists?
   puts "Line item cancelled"
else
   puts "****Line item not cancelled****"
end

You could also combine a few of the regular expression approaches below (mostly those from Kinofrost), with the idea of narrowing it down to looking just inside a single cell within the table. That should be faster, and less prone to a false alert should the words 'Current' and 'Cancelled' occur in that order with anything between them, elsewhere on the page.

if browser.table(:class, 'basic-table').cell(:text, /Current.*Cancelled/).exists?
   puts "Line item cancelled"
else
   puts "****Line item not cancelled****"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文