Unable to perform click action in selenium python

24,715

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.

Share:
24,715
Zeinab Abbasimazar
Author by

Zeinab Abbasimazar

Looking to attain a challenging and responsible position as a software engineer and software analyst in telecommunication and software industry which effectively utilizes my personal, professional and educational skills and experiences. I’m also looking forward to learn and experience more on big data concepts/solutions.

Updated on July 24, 2020

Comments

  • Zeinab Abbasimazar
    Zeinab Abbasimazar almost 4 years

    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.

  • Lawrence DeSouza
    Lawrence DeSouza about 10 years
    What is driver here? Moreover what is webdriver?
  • Mark K
    Mark K almost 9 years
    @Zeinab Abbasi, this is great! shall be "el = driver.find_element_by_id("someid")"?
  • Zeinab Abbasimazar
    Zeinab Abbasimazar almost 9 years
    @MarkK you're absolutely right! It's a typo! Thank you!
  • m3nda
    m3nda over 8 years
    @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().