Failed to execute click action in selenium python

I am writing a test script using selenium in Python. I have a webpage containing an object in the form of a tree, for example:

enter image description here

I want to go through the menu to go to the desired directory. The corresponding HTML for plus / minus signs is as follows:

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

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

I can determine if there is a plus or minus just by checking the src attribute of the img tag. I can also easily access the parent tag a using .find_element_by_xpath("..") .

The problem is that I cannot perform the action of clicking on not an img tag a.

webdriver.Actions(driver).move_to_element(el).click().perform() tried webdriver.Actions(driver).move_to_element(el).click().perform() ; but it didnโ€™t work.

I think I should mention that there is no problem accessing the elements, since I can print all their attributes; I just canโ€™t take action on them. Any help?

EDIT 1:

Here is the js code to collapse and expand 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 several hours and found that there was a problem with triggering Javascript events and click actions from the web driver. Also, I have a span tag on my webpage with an onclick event and I also have this problem.

+7
python javascript-events dom-events selenium click
source share
1 answer

After some attempts, such as .execute_script("changeTree();") , .submit() , etc., I solved the problem using the ActionChains class. Now I can click on all elements that they have java-script events like onclick . The code I used is as follows:

 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 do not know if this happened only for me or what, but I found out that I had to find the element immediately before the key command; otherwise, the script does not perform the action. I think that it will be connected with the elements of staling or something like that; In any case, thank you all for your attention.

+15
source share

All Articles