2

I want to use the Watir to click a link that looks like a button attached the image.

I use the following method,but doesn't works:

@browser.div(:id,"NetworkAnalysisTabPanel").div(:index,1).div(:index,1).ul(:index,1).li(:index,1).link(:index,2).click

Note:

@browser.div(:id,"NetworkAnalysisTabPanel").div(:index,1).div(:index,1).ul(:index,1).li(:index,1).link(:index,2).flash

is working fine but click is not working in IE and FF

Link looks like this:

link

And HMTL like this:

html

Note: I am able to click on the element using selenium IDE with clickAt method

  • 1
    If I could give you +100 for including HTML in your "I need to click" question I would. – kinofrost Nov 03 '11 at 15:30
  • I'd give him even more if the HTML was posted as a code block and not an image, but still the image is better than the nothing that is all to common. – Chuck van der Linden Nov 03 '11 at 17:02
  • Actually we might have Z to thank for the embedded stuff, check the edit history.. – Chuck van der Linden Nov 03 '11 at 17:27
  • @ChuckvanderLinden I just fixed the images, they were there but not displayed – Željko Filipin Nov 04 '11 at 10:30
  • I do wonder why people don't post the HTML more often, I wonder if better or more high-profile tutorials are needed for finding elements in watir/w-webdriver. Also, I didn't know Zelkjo goes by Z, that's awesome. I need to tune into the podcast more often. – kinofrost Nov 04 '11 at 14:25
  • @kinofrost, Z is the nickname I've bestowed on him because I am lazy and have not memorized how to spell his name, especially with the proper diacritic (accent) marks.. He's never said he dislikes it, so I keep doing it ;-) He's also the only high profile Watir group person who's name starts with that letter, so it works. If his name was Jeljko I'd be forced to learn to spell it {smirk} – Chuck van der Linden Nov 04 '11 at 19:43
  • Example site with what looks to be basically the same code: http://examples.ext.net/Examples/TabPanel/Basic/TabMenu/ – Chuck van der Linden Nov 04 '11 at 20:44

2 Answers2

1

Try this (not tested):

browser.link(:class => "x-tab-strip-menu").click

If you can flash the link, but click does not do what you want, see this: How to find out which JavaScript events fired?

Community
  • 1
  • 1
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
0

FYI what you have are links that are using standard background images controlled via CSS magic that keys on the class of the link to know what background to set. That's where the image comes from, and why you don't see it as part of the link in the HTML.

In that control, each tab is a list item element (li) in an unordered list (ul), and each list item has an ID, so that's the easiest way to tell it which tab you are trying to click inside.

Try identifying things starting with the LI that is the tab container, as within that container there is only one instance of each link of a given class. Of the 4 links, only one is without any kind of easy identifier, and if you need to click that one you'd need to use :index, but for the other 3 links using :class ought to work. This should result in code that is less brittle and subject to being broken if the order of tabs changes, or the page is refactored.

@browser.li(:id,"NetworkAnalysisTabPanel__ext-comp-1038").link(:class, "x-tab-strip-menu").click

If the number at the end of the ID is subject to change, you can try a regular expression to match the part you can predict and is unique from the others

@browser.li(:id,/NetworkAnalysisTabPanel__ext-comp-/).link(:class, "x-tab-strip-menu").click

If you can reliably identify the object and use .flash but .click does not seem to do anything, you may have to use .fire_event('onclick') instead or .click.

@browser.li(:id,/NetworkAnalysisTabPanel__ext-comp-/).link(:class, "x-tab-strip-menu").fire_event('onclick')

If that does not work, then you need to start experimenting with likely events that the control might be looking for (which will not necessarily show up in the HTML btw.. it may be in javascript or CSS etc)

UPDATE This is where having an live example of the control that we can interact with is critical. doing some googling on the class names I was able to find one here and that let me play with it a little, and what I discovered is that it is looking for onmousedown. so, on that site, this works

browser.li(:id, 'TabPanel1__ctl07').link(:class, 'x-tab-strip-menu').fire_event('onmousedown')

Now since those ID's may not be the best identifier, a bit more digging (using .text on the li that holds the tab parts) found me some text, which in a menu like that ought to be unique.. SO, we can change this to make things a bit more robust and clearer as to what tab I'm clicking on (this will also be less subject to breaking if the tabs change around.

browser.li(:text, 'Menu 1').link(:class, 'x-tab-strip-menu').fire_event('onmousedown')

Lastly, since the click is causing client side code to execute, you may need a brief pause (a one or two second sleep) to wait for that portion of the page to re-render etc.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
  • using :class is not working i am getting element not found error,i used the following code to locate ===@browser.div(:id,"NetworkAnalysisTabPanel").div(:index,1).div(:index,1).ul(:index,1).li(:index,1).link(:index,2).click == i cant use the @browser.li(:id,"NetworkAnalysisTabPanel__ext-comp-1038").link(:class, "x-tab-strip-menu").click because id of li is dynamic – Naveen Kandakur Nov 04 '11 at 10:01
  • Is there a portion of that class name that is static and predictable and different from the others? if so you may be able to use a regular expression to match the LI element. Long lists like you have are not preferred as they tend to be brittle and break if the page changes even a tiny bit in that area. – Chuck van der Linden Nov 04 '11 at 19:19
  • @NaveenKandakur, found a solution, at least it works on a demo site that appears to have the same basic code. See the portion of the answer after 'UPDATE' – Chuck van der Linden Nov 06 '11 at 05:13
  • Fired onmousedown event and gave sleep time of 3sec but drop down is not displaying when i tried the same locator with flash, able observe yellow flash on element – Naveen Kandakur Nov 08 '11 at 13:16
  • I don't know what to tell you other than to perhaps experiment with other possible events. Also perhaps a series of events such as onmouseover followed by on mousedown (see: http://stackoverflow.com/questions/3787555/how-to-find-out-which-javascript-events-fired) The sample site I found is the closest thing I could find to the control your code is using, and the stuff I gave you worked against that site for me. Without direct access to your site there's little more I or anyone else can do. That or contact the vendor of the code library and talk to their support folks. – Chuck van der Linden Nov 08 '11 at 17:55