如何使用 watir-webdriver 在我正在处理的页面上自动生成 google 链接?
我正在处理的页面链接是 http://www.whatcar.com/ car-news/subaru-xv-review/260397
我正在尝试自动执行“点击谷歌链接”,但运气不佳并且不断收到错误。
链接 HTML:
<a tabindex="0" role="button" title="" class="s5 JF Uu" id="button" href="javascript:void(0);" aria-pressed="false" aria-label="Click here to publicly +1 this."></a>
我的代码:
@browser.link(:class, "s5 JF Uu").click
错误消息:
unable to locate element, using {:class=>"s5 JF Uu", :tag_name=>"a"} (Watir::Exception::UnknownObjectException)
./step_definitions/11.rb:12:in `/^On the page I click 'Twitter' , Facebook and Google button$/'
11.feature:8:in `When On the page I click 'Twitter' , Facebook and Google+ button'
Page link I am working on is http://www.whatcar.com/car-news/subaru-xv-review/260397
I am trying to automate 'clicking the google link' but am having no luck and keep receiving an error.
Link HTML:
<a tabindex="0" role="button" title="" class="s5 JF Uu" id="button" href="javascript:void(0);" aria-pressed="false" aria-label="Click here to publicly +1 this."></a>
My code:
@browser.link(:class, "s5 JF Uu").click
Error message:
unable to locate element, using {:class=>"s5 JF Uu", :tag_name=>"a"} (Watir::Exception::UnknownObjectException)
./step_definitions/11.rb:12:in `/^On the page I click 'Twitter' , Facebook and Google button$/'
11.feature:8:in `When On the page I click 'Twitter' , Facebook and Google+ button'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该链接位于框架内。为了让它更有趣,每次刷新页面时,框架
id
都是不同的。I1_1323429988509
和I1_1323430025052
是框架。由于I1_
部分始终相同,并且没有其他框架具有该部分,因此您可以像这样访问框架:由于框架内只有一个链接:
您可以像这样单击链接:
或者如果您希望更明确
这将打开一个新的浏览器窗口,新的挑战就在这里! :)
The link is inside a frame. To make it even more fun, frame
id
is different every time the page is refreshed.I1_1323429988509
andI1_1323430025052
is the frame. SinceI1_
part is always the same, and no other frame has that, you can access the frame like this:Since there is only one link inside the frame:
You can click the link like this:
Or if you prefer to be more explicit
That will open a new browser window, and a new challenge is here! :)
技术答案:
您链接的页面上的按钮的类别对我来说与您列出的类别不同。看起来它的行为有所不同,具体取决于本地计算机上的 cookie(在 Watir 驱动的 Firefox 或 IE 会话期间将不存在)。
您需要找到一个非动态的不同元素来挂钩。
道德答案:
您试图通过社交媒体自动推广在线文章是值得怀疑的。 Watir/Watir-Webdriver 不是垃圾邮件机器人,您使用的服务明确禁止使用自动化/机器人。
The technical answer:
The class of the button on the page that you linked is different for me than the class that you list. It looks like it behaves differently based on the cookies on your local machine (which would be absent during a Watir-driven Firefox or IE session).
You would need to find a different element that is not dynamic to hook into.
The ethical answer:
It is questionable that you are attempting to automate the promotion of online articles through social media. Watir/Watir-Webdriver is not a spam bot, and the services you are using specifically prohibit the use of automation/bots.
该“按钮”链接位于 iframe 内。阅读 watir Wiki 如何处理框架中的内容。如果这还不足以使其正常工作,请使用修改后的代码和错误等编辑答案,我们可以从那时起继续解决。
That 'button' link is inside an iframe. Read on the watir Wiki how to deal with stuff in frames. If that's not enough to get it working please edit the answer with revised code and error etc and we can work it forward from that point.