Selenium webdriver点击vs action.click有什么区别? [英] Selenium webdriver click vs action.click whats the difference?

查看:2081
本文介绍了Selenium webdriver点击vs action.click有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上弹出对话框,我正在自动化,由于某种原因,它不会点击对话框中的按钮,但与一切都很好的互动。它也是识别的按钮没有问题,我可以看到当看到它运行它似乎点击按钮,因为颜色改变,所以我有点卡住。

I was struggling with a popup dialog in a website I'm automating, for some reason it wouldn't click a button in the dialog but interacted fine with everything else fine. It was also identifying the button without issue and I could see when watching it run that it did seem to click the button as the colour changed, so I was a bit stuck.

我的代码原来看起来很明显: -

My code originally looked pretty obvious: -

WebElement targetElement = waitforElement(threadSesh);
targetElement.click();

但是当我把它改成下面的时候,似乎都能工作得很好!

But when I changed it to the following all seemed to work absolutely fine!

WebElement targetElement = waitforElement(threadSesh);
Actions action = new Actions(threadSesh.driver);
action.click(targetElement);
action.perform();

任何人都可以告诉我为什么一个方法不工作,而另一个工作正常?此外,无论是否有帮助,网站是使用twitter bootstrap构建的,如果这有什么区别,但按钮本身是非常基本的,像这样。

Can anyone tell me why one method doesn't work while the other works fine? Also, whether it helps or not, the site is built using twitter bootstrap if that makes any difference but the button itself was pretty basic, like so.

<div class="modal hide fade in" style="width: 481px; margin-left: -241px; padding: 15px; display: block;" role="dialog" data-backdrop="static" data-keyboard="false" aria-hidden="false">
...
<div class="modal-footer">
<a id="save-EditDetails" class="btn btn-small btn-info" type="button" style="margin-right: 7px; padding-left: 15px; padding-right: 15px;" data-bind="click: ok" href="#">Save</a>
</div>
</div>


推荐答案

我不知道你的具体情况,是WebElement点击方法和动作点击方法之间的几个差异。动作的点击是一个很多dumber,它几乎只是发送点击事件到你传递的元素(位置)。它不关心元素,它只是点击和向前移动,而webelement点击是一个阻塞调用(不总是,检查引用),它也有前提条件,如WebElement被点击必须可见。此外,webElements的点击是一个void方法,操作点击返回对您正在使用的操作的引用。有关详情,请访问这里此处

I am not sure about your specific case, but there are several differences between WebElement click method and Actions click method. Actions' click is a lot dumber, it pretty much just sends the click event to the element (location) that you pass in. It does not care about the element, it just does the click and moves forward whereas webelement click is a blocking call (not always, check the references) and it also has preconditions like the WebElement to be clicked must be visible. Also, webElements' click is a void method, actions click returns reference to the Actions you are using. For more information check here and here.


看看你发布的标记,它可能完全错了,因为我不是一个老板的bootstrap CSS,模式隐藏褪色,特别是 fade in 部分有看起来可疑。您确定当您发送webelement.click(),您的元素处于可点击状态吗?发生了什么?没有?然后再次,如果动作点击可靠地工作,为什么不只是去与它,我的意思是,如果有效果,为什么解决它?

edit. Looking at the markup you posted, and it can be totally wrong as I am not a boss on bootstrap CSS, the modal hide fade in and especially the fade in part there looks suspicious. Are you sure that when you send the webelement.click(), your element is in clickable state? What happens? Nothing? Then again, if the actions click is reliably working, why not just go with it, I mean, if something works, why fix it?

这篇关于Selenium webdriver点击vs action.click有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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