使用 Ruby webdriver (Selenium 2.0) 单击 javascript 链接

发布于 2024-09-06 21:41:26 字数 843 浏览 0 评论 0原文

使用 ruby​​,如何让 webdriver 单击 javascript 链接?

我试图点击的链接是: 管理

我是否能够触发 javascript按键事件?如果是这样,有人知道这样做的语法吗?我很难找到一个好的 webdriver 参考资料——尽管我知道随着 Beta 版本的结束,它可能会变得更好。

我还研究了 firewatir 和 mechanize。如果您有理由认为其中一种方法是更好的方法,请告诉我并说明原因。它需要能够单击 JavaScript 链接并从电子表格或 csv 文件提交表单数据。

感谢您的帮助!

require 'rubygems'
require 'watir-webdriver'

ff=Watir::Browser.new(:firefox)
ff.goto("http://my-web-address")

#Put your user name. 
ff.text_field(:name,"user").set("my-username")

#Put your password.
ff.text_field(:name,"pass").set("my-passwd")

#Click Sign In button.
ff.button(:value,"Login").click

#Click on Administration tab. <-- This does not work!!
ff.link(:text, "Administration").click

Using ruby, how can I get webdriver to click on a javascript link?

The link I'm trying to click on is:
<a class="TabOff" href=" javascript:showConfirm('/campustoolshighered/k12_admin_admin_menu.do');">Administration </a>

Would I be able to trigger the javascript with a keyPress event? If so, does anyone know the syntax for doing that? I've had trouble finding a good reference for webdriver -- though I understand that it'll probably get better as it moves out of beta.

I've also looked at firewatir and mechanize. If you have reason to think that one of these would be a better approach then please let me know and why. It needs to be able to click on javascript links and submit form data from a spreadsheet or csv file.

Thanks for the help!

require 'rubygems'
require 'watir-webdriver'

ff=Watir::Browser.new(:firefox)
ff.goto("http://my-web-address")

#Put your user name. 
ff.text_field(:name,"user").set("my-username")

#Put your password.
ff.text_field(:name,"pass").set("my-passwd")

#Click Sign In button.
ff.button(:value,"Login").click

#Click on Administration tab. <-- This does not work!!
ff.link(:text, "Administration").click

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

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

发布评论

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

评论(1

春风十里 2024-09-13 21:41:26

点击确实会触发javascript。您确定它确实找到了链接吗?

您的 HTML 源代码在“管理”一词后显示空格字符
也许就是这个原因。

检查是否

ff.link(:text, "Administration").exists?

返回true。

或者尝试不同的匹配:

ff.link(:class => 'tabOff').click

注意:您也可以对 :text 使用正则表达式,例如

ff.link(:text, /Administration/)

希望有所帮助。

Click will indeed trigger the javascript. Are you sure It is actually finding the link?

Your HTML source code shows a space characters after the word 'Administration '
Maybe that the cause.

Check if

ff.link(:text, "Administration").exists?

returns true.

Or try a different match:

ff.link(:class => 'tabOff').click

Note: You can also use regexp for the :text as well, such as

ff.link(:text, /Administration/)

Hope to help.

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