无法在 selenium python 中执行点击操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19769532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to perform click action in selenium python
提问by Zeinab Abbasimazar
I'm writing a test script using selenium in python. I have a web-page containing a tree-view object like this:
我正在 python 中使用 selenium 编写测试脚本。我有一个包含树视图对象的网页,如下所示:
I want to traverse over the menu to go to the desired directory. Respective HTML code for plus/minus indications is this:
我想遍历菜单以转到所需的目录。加/减指示的相应 HTML 代码是这样的:
<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
.
src
图像的属性可以是open.gif
或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("..")
.
我可以通过简单地检查标签的src
属性来检测天气的img
好坏。我也可以很容易地访问父标签,a
通过使用.find_element_by_xpath("..")
。
The problem is that I can't perform the click action not on the img
nor the a
tag.
问题是,我不能在没有进行点击操作img
,也没有a
标签。
I'v tried webdriver.Actions(driver).move_to_element(el).click().perform()
; but it did not work.
我试过了webdriver.Actions(driver).move_to_element(el).click().perform()
;但它没有用。
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:
编辑 1:
Here's the js code for collapsing and expanding the tree:
这是用于折叠和展开树的 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:
编辑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.
我在谷歌上搜索了几个小时,发现在触发 Javascript 事件和来自网络驱动程序的点击操作方面存在问题。另外span
,我的网页中有一个标签,上面有一个onclick
事件,我也有这个问题。
采纳答案by Zeinab Abbasimazar
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:
经过一些尝试,例如.execute_script("changeTree();")
,.submit()
等,我已经通过使用ActionChains
该类解决了该问题。现在,我可以单击所有具有 java 脚本事件的元素作为onclick
. 我使用的代码是这样的:
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.
我不知道它是否只是发生在我身上或者是什么,但我发现我应该在键盘命令之前找到元素;否则脚本不会执行操作。我认为这与陈旧元素或类似的东西有关;无论如何,感谢大家的关注。