简体   繁体   中英

Using Ruby webdriver (Selenium 2.0) to click on a javascript link

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM