gpt4 book ai didi

ruby - 如何检查
中的文本?

转载 作者:数据小太阳 更新时间:2023-10-29 07:42:46 25 4
gpt4 key购买 nike

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

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

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

enter image description here

最佳答案

我真的建议使用 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

类似于底部两个语句之一的内容将为您提供可用的 true/false。

关于ruby - 如何检查 <div> 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5285776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com