使用 Ruby webdriver (Selenium 2.0) 单击 javascript 链接
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
点击确实会触发javascript。您确定它确实找到了链接吗?
您的 HTML 源代码在“管理”一词后显示空格字符
也许就是这个原因。
检查是否
返回true。
或者尝试不同的匹配:
注意:您也可以对 :text 使用正则表达式,例如
希望有所帮助。
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
returns true.
Or try a different match:
Note: You can also use regexp for the :text as well, such as
Hope to help.