无法在硒蟒中执行点击操作 [英] Unable to perform click action in selenium python

查看:115
本文介绍了无法在硒蟒中执行点击操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中使用selenium编写测试脚本。我有一个网页包含这样的树视图对象:





我想遍历菜单,转到所需的目录。用于加/减指示的各个HTML代码是:

 < a onclick =changeTree('tree','close。 gif','open.gif');> 
< img id =someidsrc =open.gif/>
< / a>

图像的 src 属性可以 open.gif close.gif



通过简单地检查 img 标签的 src 属性,我可以检测到天气有加或减。我也可以使用 .find_element_by_xpath(..)轻松访问父标记 a



问题是我无法对 img 执行点击操作,也不能执行 a 标记。



我尝试过 webdriver.Actions(driver).move_to_element(el).click()。perform();但它没有工作。



我想我应该提到访问元素没有问题,因为我可以打印所有的属性;我只是不能对他们执行操作。任何帮助?



编辑1:



这里是js代码折叠和扩展树:

  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: / p>

我googled了几个小时,我发现有一个问题,触发javascript事件和来自web驱动程序的单击操作。此外,我有一个 span 标签在我的网页中有一个 onclick 事件,我也有这个问题。

解决方案

在尝试 .execute_script(changeTree();) .submit()等,我已经通过使用 ActionChains 类解决了这个问题。现在,我可以点击所有元素,他们有java脚本事件 onclick 。我使用的代码是:

 从selenium import webdriver 
driver = webdriver.Firefox $ b driver.get('someURL')
el = driver.find_element_by_id(someid)
webdriver.ActionChains(driver).move_to_element(el).click(el).perform $ b

我不知道它是发生在我身上还是什么,但我发现我应该找到元素在键命令之前;否则脚本不执行操作。我认为这将是与staling元素或类似的东西;无论如何,感谢他们的注意。


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

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.

解决方案

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.

这篇关于无法在硒蟒中执行点击操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆