6

Can anyone explain why/how in Selenium IDE, for an item I want to click on, the click doesn't work, however using mouseOver, mouseDown, mouseUp and mouseOut on the same Target does work. I have even tried to use multiple of those above, e.g.:

waitForElementPresent
mouseDown
mouseOver
mouseDown
click
mouseOut

This managed to work for me on something else, when using click didn't work. I also don't seem to be getting any errors and don't know what to do. Any help is appreciated.

UPDATE:
I clicked on the Find button in Selenium IDE and it highlighted the element, with all 6 of those Commands above, but it never actually clicks it when I run any of the commands or the case.

dhruveonmars
  • 409
  • 1
  • 11
  • 24
  • Can you manually click on the element and it behaves as expected? Do you prevent JavaScript errors from being displayed which might give you a hint? – Juergen Riemer Mar 21 '13 at 11:56
  • @JuergenRiemer Yes, I can manually click on it and it does what it should. In Selenium-IDE however, I get no errors. If I change the Target to the wrong thing I do get errors, and as previously stated, when I use `mouseOver`, `mouseUp`, `mouseDown` and `mouseOut` on the item instead of click, it does what it should. – dhruveonmars Mar 21 '13 at 12:07
  • What element is it you want to click on? – Juergen Riemer Mar 21 '13 at 12:28
  • I am clicking on a div – dhruveonmars Mar 21 '13 at 12:33
  • You can also fall back on Javascript to perform actions, e.g. ` storeEval var dom=selenium.browserbot.getUserWindow(); dom.document.getElementById( 'foo' ).click() void ` – Juergen Riemer Mar 21 '13 at 13:17
  • @JuergenRiemer I am using Selenium-IDE at the moment, would I type this into Selenium-IDE in the same manner, so Command=`storeEval`, Target=`var dom=selenium.browserbot.getUserWindow(); dom.document.getElementById( 'foo' ).click()` and value=`void` ? As I didn't think Selenium needed to be written as selenium. Or do you mean write it like that in a javascript page? – dhruveonmars Mar 21 '13 at 15:06
  • Sorry for the lack of explanation, I posted the HTML representation of the command that you find in the file Selenium IDE produces upon save. You could also enter it in the IDE itself. Add new line of command and then first line: storeEval, 2nd line: dom=..... 3rd line: void (this is just a name to know you are not interested in the stored value – Juergen Riemer Mar 21 '13 at 16:50
  • @JuergenRiemer I put what you said and got this: '_[error] Threw an exception: dom.document.getElementById(...) is null_' – dhruveonmars Mar 22 '13 at 10:07
  • you have to replace `foo` with the id of your DOM element. – Juergen Riemer Mar 22 '13 at 19:10
  • @JuergenRiemer It doesn't have an ID, can I use `getElementByClassName`? – dhruveonmars Mar 25 '13 at 09:38
  • This should be possible for Firefox, IE doesn't support this AFAIK at least until version 8. Bear in mind you'll get back a list of objects. Alternatively you can use jQuery if available. Can you for the sake of quick proof add some id? This might make it much easier to track down the issue. – Juergen Riemer Mar 25 '13 at 10:32
  • @JuergenRiemer I am not allowed to make changes to the website so cannot add an ID. Also, I tried what you stated earlier, but the `getElementByClassName` and get the error _Threw an exception: dom.document.getElementByClassName is not a function_ when I execute the command, and when I click the *Find* button, I get the error _locator not found: dom=selenium.browser....click_ – dhruveonmars Mar 25 '13 at 10:41
  • `getElementsByClassName` (with `s`) this indicates you get back a list of objects. That is to reference the 3rd P tag you need to use: `document.getElementsByTagName("p")[2]` – Juergen Riemer Mar 25 '13 at 10:45
  • @JuergenRiemer since I am not using any `p` tags, would I do `document.getElementsByTagName("div")[a specific number, depending on where the div is]`? If so, would I use FirePath to check the number? – dhruveonmars Mar 25 '13 at 10:50
  • I don't know firepath but that's basically what you'd do.. I think you can also get the index from selenium IDE itself, just check the dropdown of "Target" for an element you should get some xpath info as well. – Juergen Riemer Mar 25 '13 at 11:00
  • @JuergenRiemer So if Selenium IDE gave me this: `//div[14]/div/div[4]/div[7]` would I just add them up, and then enter that, so: `document.getElementsByTagName("div")[26]`? – dhruveonmars Mar 25 '13 at 12:30
  • @JuergenRiemer I did that, and when I executed the Command, nothing happened, no error or anything. When I clicked the **Find** button, I got the error: `locator not found: dom=selenium.browserbot.getUserWindow(); dom.document.getElementsByTagName( 'div' )[26].click, error = TypeError: e.scrollIntoView is not a function` – dhruveonmars Mar 25 '13 at 12:41
  • Try with parenthesis: `dom.document.getElementsByTagName( 'div' )[26].click()` – Juergen Riemer Mar 25 '13 at 12:47
  • @JuergenRiemer When I execute it, it says `Executing:` and then it says `script is...` However, when I click the **Find** Button, I get the error `locator not found`. – dhruveonmars Mar 25 '13 at 12:59
  • The "storeEval" entry, JavaSript command respectively can't be found by the locator since it is no DOM node. Is this webpage accessible online? I think w/o any code we slowly reach the end of options. – Juergen Riemer Mar 25 '13 at 13:23
  • @JuergenRiemer Yes, the webpage is online. Anyways, thanks for your help. – dhruveonmars Mar 25 '13 at 13:28
  • tell me the URL and where to find that DIV element, I'll have a look – Juergen Riemer Mar 25 '13 at 13:48
  • The website requires a login, and a login can only be made by an Admin, of which I am not. But here is the code copied: `
    `
    – dhruveonmars Mar 25 '13 at 13:59
  • Hmm that doesn't help too much; perhaps you have a look at the documenation to get some ideas: http://docs.seleniumhq.org/docs/02_selenium_ide.jsp – Juergen Riemer Mar 25 '13 at 14:49
  • @JuergenRiemer I got it working and have posted what I used below. Even though it seems like a bit of an odd way to do it. Better than nothing. Thanks for all your help though. – dhruveonmars Mar 25 '13 at 15:02
  • From my experience in selenium, it behaves poorly when items have no ids. Try to get the element by the exact xpath. Your xpath, by class, can return more than one element and selenium might no like it. – jagra Mar 25 '13 at 15:03

5 Answers5

1

there are all sorts of implementations for click out there, some use Javascript that listen to the mouse down event, some use the mouse up or click or mouse over the div or td they implemented as a clickable element and then the JS start working and you get the click action expected,

therefor there is no one correct event you should launch, rather test them all or execute them all,

i use the following commands and it works for most elements that doesn't work with a simple 'click' command:

    <tr>
    <td>focus</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td></td>
</tr>
<tr>
    <td>mouseOver</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td></td>
</tr>
<tr>
    <td>mouseMoveAt</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td></td>
</tr>
<tr>
    <td>mouseDown</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td></td>
</tr>
<tr>
    <td>mouseDownAt</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td></td>
</tr>
<tr>
    <td>clickAt</td>
    <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
    <td>0,0</td>
</tr>

you can try more events until it works, if you are still experiencing problems then make sure your element path is correct (use the selenium firefox IDE and press "Find" button while the FireBug is open and it will colorize the element and it's Html code with yellow glowing color so you can make sure the element path is correct),

another trick is to right click the element and click "Wait for Text..." and copy the path from that command to the click and other commands i stated earlier

if element that you want to click is hidden (for instance a floating layer html) then you can change its attribute display:none to display:block in the firebug (temporary) and that way you can use the rightclick trick to extract the exact path

Shaybc
  • 2,628
  • 29
  • 43
1

If you record a "Selenium Builder" script on a page that has an iFrame, I don't believe the recorder will record the switchTo event even though the mouse will have no problem accessing it. So, on replay, without the Selenium switchTo, a selenium even wont be able to click the element unless you manually add a switchTo frame call.

djangofan
  • 28,471
  • 61
  • 196
  • 289
0

Try accessing the location_once_scrolled_into_view member variable (e.g., as in the following code).

ScrolledPosition = element.location_once_scrolled_into_view
element.click()

For some reason, that quite often allows the click to work even if your code doesn't actually use the position stored there explicitly.

RobH
  • 1,607
  • 2
  • 24
  • 44
  • Do I input it exactly like that in Selenium IDE? Where the command would be `ScrolledPosition` and the target would be the rest? – dhruveonmars Mar 21 '13 at 16:32
  • ScrolledPosition is simply a variable that takes the value of element.location_once_scrolled_into_view. In this case, its simply a throwaway, because it isn't actually being used other than as a way to access the location_once_scrolled_into_view member variable in order to get the click() to work. My sample code is in Python, by the way. If you're using a different language, you might want to check if that member variable has a slightly different name (e.g., using camel case instead of underscores). – RobH Mar 21 '13 at 16:41
  • I'm not really sure of what you're saying. But, the source code for Selenium IDE is HTML. – dhruveonmars Mar 22 '13 at 10:13
  • OK, we may be talking about two separate things here, then. I was thinking that you were using Selenium IDE to write and run code that uses Selenium Web Driver, which was written in Java and has wrappers in a few other languages (including Python). Sorry I can't be of any further help. – RobH Mar 22 '13 at 15:41
  • you need to test all the mouse and click events to successfully click your button – Shaybc Mar 28 '14 at 12:21
0

I was able to get Selenium IDE to click the item, even though it doesn't seem to be the correct method in doing so. Below is the HTML for Selenium IDE.

<tr>
     <td>mouseDown</td>
     <td>css=div.dhtmlx_wins_btns_button.dhtmlx_button_close_default</td>
     <td></td>
</tr>
<tr>
     <td>clickAt</td>  
     <td>//div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
     <td>0,0</td>
</tr> 

Or you could use //div[@class='dhtmlx_wins_btns_button dhtmlx_button_close_default'] instead of the CSS locator.
I would still appreciate if someone has a better way of getting it to click than this as it still seems like the wrong way to go about it.

dhruveonmars
  • 409
  • 1
  • 11
  • 24
  • this might work and might not work since the event that the javascript is listening to might be click and perhaps be mouseDown etc. you need to test all the mouse and click events to successfully click your button – Shaybc Mar 28 '14 at 12:21
0

Also had same problem with clicking at element <td>. I used method selenium.clickAt(locator, coordString) f.e.:

selenium.clickAt("element id - easily localized by firebug / firepath [firefox addon]","1.1");
  • this might work and might not work since the event that the javascript is listening to might be click and perhaps be mouseDown etc. test them all until success – Shaybc Mar 28 '14 at 12:22