8

I'm writing a test script using selenium in python. I have a web-page containing a tree-view object like this:

enter image description here

I want to traverse over the menu to go to the desired directory. Respective HTML code for plus/minus indications is this:

<a onclick="changeTree('tree', 'close.gif', 'open.gif');">
    <img id="someid" src="open.gif" />
</a>

The src attribute of the image can be either open.gif or close.gif.

I can detect weather there is a plus or minus by simply checking the src attribute of the img tag. I can also easily access to the parent tag, a, by using .find_element_by_xpath("..").

The problem is that I can't perform the click action not on the img nor the a tag.

I'v tried webdriver.Actions(driver).move_to_element(el).click().perform(); but it did not work.

I think I should mention that there is no problem in accessing the elements, since I can print all their attributes; I just can't perform actions on them. Any help?

EDIT 1:

Here's the js code for collapsing and expanding the tree:

function changeTree(tree, image1, image2) {
    if (!isTreeviewLocked(tree)) {
        var image = document.getElementById("treeViewImage" + tree);
        if (image.src.indexOf(image1)!=-1) {
            image.src = image2;
        } else {
            image.src = image1;
        }

        if (document.getElementById("treeView" + tree).innerHTML == "") {
            return true;
        } else {
            changeMenu("treeView" + tree);
            return false;
        }
    } else {
        return false;
    }
}

EDIT 2:

I Googled for some hours and I found out that there is a problem about triggering the Javascript events and the click action from web-driver. Additionally I have a span tag in my web-page that has an onclick event and I also have this problem on it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
  • I'm assuming no errors are being thrown? Are there nearby elements that have javascript attached to them? – Nathan Merrill Nov 04 '13 at 14:22
  • @MrTi, yes there is no error and of course there are some js codes. I don't how can it be related. – Zeinab Abbasimazar Nov 04 '13 at 14:28
  • Can you post the javascript code that is listening for the plus/minus click event? – Alex Wittig Nov 04 '13 at 14:44
  • @MrTi, I posted the js code in the question, but could please explain me the relationship between js code and the problem? – Zeinab Abbasimazar Nov 04 '13 at 14:53
  • I am looking for the code that calls `changeTree()` when a click happens. That will help make sure that your selenium code is clicking the right thing. – Alex Wittig Nov 04 '13 at 15:00
  • Because those elements with the javascript are probably the ones you want to click on, NOT the image elements. The javascript is probably what is opening the folders, so if each openable folder has a nearby javascript element, then find a selector that will click on that one, and it should work. – Nathan Merrill Nov 04 '13 at 15:01
  • As a note, I'm not looking for the separate javascript file, just the element with javascript in it. – Nathan Merrill Nov 04 '13 at 15:02
  • Ok, if I understand what you two said, I should answer that the element that it's `onclick` event is `changeTree()` is the `a` tag that I mentioned in the question. I updated the HTML code. – Zeinab Abbasimazar Nov 05 '13 at 05:24
  • In a few cases whilst writing my own Webdriver test cases I have had, rarely I'll admit, had to 'bypass' the button and directly call the Javascript that is the result of that click. Have you tried that at all? It's not a perfect solution I know but it may assist you in the short term. – Mark Rowlands Nov 05 '13 at 09:02
  • @MarkRowlands, I tried to use `execute_script`, but in my case, it occurs sometimes that the click results an alert and with this command I should close the alert manually to continue the script. Consequently I solved it with the `ActionChains` class. I've posted the answer. – Zeinab Abbasimazar Nov 05 '13 at 10:02

1 Answers1

20

After some tries like .execute_script("changeTree();"), .submit(), etc, I have solved the issue by using the ActionChains class. Now, I can click in all elements that they have java-script events as onclick. The code that I have used is this:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('someURL')
el = driver.find_element_by_id("someid")
webdriver.ActionChains(driver).move_to_element(el).click(el).perform()

I don't know if it occurred just to me or what, but I found out that I should find the element right before the key command; otherwise the script does not perform the action. I think it would be related to staling elements or something like that; anyway, thanks all for their attention.

Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
  • 1
    What is driver here? Moreover what is webdriver? – Lawrence DeSouza Apr 10 '14 at 00:33
  • @Zeinab Abbasi, this is great! shall be "el = driver.find_element_by_id("someid")"? – Mark K Jul 21 '15 at 02:51
  • @MarkK you're absolutely right! It's a typo! Thank you! – Zeinab Abbasimazar Aug 02 '15 at 07:34
  • @LawrenceDeSouza `driver` is the name you set to `webdriver.Firefox()` instance. `webdriver` is the class webdriver at `selenium` module. The `ActionChains` is used without set it to a name like it does with the `driver`, so it may be confusing to understand. U could do too `eldriver = webdriver.ActionChains(etcetc).move(etc).click(etc)` then `eldriver.perform()`. – m3nda Jan 19 '16 at 00:25