如何在 Watir 中获取图像标题属性?

发布于 2024-09-27 23:20:39 字数 844 浏览 2 评论 0原文

给出以下 HTML 代码(我意识到这很糟糕,但这不是我目前可以解决的问题):

<img height="64" width="64" class='list_item' src="/img/icon/first.jpg" 
title="This is the first item::Completed the item "I did this first"" alt="First" />

给我一个结果 (this is an image.to_s)

name:
type:
id:
value:
disabled:
src:          /img/icon/first.jpg
width:        64
height:       64
alt:          First

注意缺少“title”元素。这实际上并没有改变(缺少标题元素)

如果我获取这些图标之一的父 div 的内容,我会得到类似的内容:

<img class="list_item" I="" did="" this="" first="" src="/img/icon/first.jpg" alt="First">

原始的损坏的 HTML 已被转换为单独的属性,但标题标签似乎已被完全剥离,并且由于它是我需要的标题标签的内容,所以我有点卡住了。

已使用 Firefox 在 Ruby 1.9.2 上的最新 Watir 上尝试过此操作。

完美世界的解决方案:我想获取图像标签的原始传输 HTML,这样我就可以解决愚蠢的双引号问题的“特殊情况”(即破解)。

足够好的解决方案:标题标签的内容。

Given the following HTML code (which, I realise, sucks, but that's not something I can currently solve):

<img height="64" width="64" class='list_item' src="/img/icon/first.jpg" 
title="This is the first item::Completed the item "I did this first"" alt="First" />

gives me a result of (this is an image.to_s)

name:
type:
id:
value:
disabled:
src:          /img/icon/first.jpg
width:        64
height:       64
alt:          First

Note lack of "title" element. This does not actually change (the lack of the title element)

If I get the contents of the parent div of one of those icons, I get something like:

<img class="list_item" I="" did="" this="" first="" src="/img/icon/first.jpg" alt="First">

The broken HTML of the original has been turned into separate attributes somewhere down the line, but the title tag appears to have been stripped completely, and since it's the contents of the title tag I need, I'm a little stuck.

This has been tried with lastest Watir on Ruby 1.9.2 using Firefox.

Perfect world solution: I'd like to get the original transmitted HTML for the image tag, so I can "special case" (ie, hack) around the stupid double-quote problem.

Good Enough Solution: the contents of the title tag.

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

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

发布评论

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

评论(3

满身野味 2024-10-04 23:20:39

Watir::Image 上实际上有一个 #title 方法。对于上述不正确的 HTML,输出将如下所示(其中“i”是 Image 对象):

i.title
=> "This is the first item::Completed the item "

这仅显示标题的一部分。

但是你可以使用 #html,然后用一些魔法从中解析出所有必要的信息:

i.html
=> "<IMG class=list_item title=\"This is the first item::Completed the item \" alt=First src=\"/img/icon/first.jpg\" width=64 height=64 first?? this did I>"

但正如上面其他答案所提到的 - 由于 HTML 不好,你无法正确地得到它。也许还有其他方法可以实现您的更大目标?

There is actually a #title method on Watir::Image. With the above incorrect HTML the output would be like this (where 'i' is the Image object):

i.title
=> "This is the first item::Completed the item "

This shows only part of the title.

But you could use #html and then parse all the necessary information out of it with some magic:

i.html
=> "<IMG class=list_item title=\"This is the first item::Completed the item \" alt=First src=\"/img/icon/first.jpg\" width=64 height=64 first?? this did I>"

But as other answers above have mentioned - you cannot get it out correctly due to the bad HTML. Maybe there's some other way to accomplish your bigger goal you're having?

月野兔 2024-10-04 23:20:39

获取标题可能不起作用,因为在该元素上设置 title 属性的方式无效。实体 " 和 < 和 > 需要在 html 属性内转义,其中 " 和 <和>分别。转义引号并重试。

getting the title probably isn't working because the way the title attribute is set on that element isn't valid. entities " and < and > need to be escaped inside html attributes, with " and < and > respectively. Escape the quotes and try again.

泡沫很甜 2024-10-04 23:20:39

不确定,但我认为 Watir 不支持图像标题。我查看了 Supported Elements 页面,标题是 x 'ed 出来。我在 RDoc for Watir::Image 类型中没有看到它 要么。

Not sure, but I don't think Watir supports image titles. I looked over the Supported Elements page, title was x'ed out. I don't see it in the RDoc for Watir::Image type either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文