如何检查内的文本?
我正在尝试访问位于 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我真的建议使用 Nokogiri 来解析内容。
像下面两个陈述之一这样的东西会给你一个可用的真/假。
I'd really recommend using Nokogiri to parse the content.
Something like one of the two bottom statements will get you a usable true/false.
这不起作用的可能原因是您正在测试的字符串包含换行符和不间断空格。
这可以工作......
或者
等等。
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...
or
etc.
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.
我相信文本位于表格内这一事实导致了此问题。
您可能会考虑深入研究表,执行以下操作:
I believe the fact that the text is inside a table is causing this problem.
You might consider drilling into the table doing:
好的,这是一个非常快速的草稿:
Okay, here's a really quick draft:
您还可以结合下面的一些正则表达式方法(主要是来自 Kinofrost 的方法),将其范围缩小到仅查看表中的单个单元格内部。如果“当前”和“已取消”这两个词按顺序出现在页面上的其他位置,并且它们之间有任何内容,那么这应该会更快,并且不太容易出现错误警报。
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.