单击使用javascript vs action vs webdriver的元素? [英] Clicking an element using javascript vs actions vs webdriver?

查看:74
本文介绍了单击使用javascript vs action vs webdriver的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用以下方法单击网络元素.

We can click web-element using the following methods.

myWebElement.click();

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", myWebElement);

Actions(driver).click(myWebElement).build().perform();

这些方法有什么区别?

推荐答案

myWebElement.click();

myWebElement.click();

Actions(driver).click(myWebElement).build().perform();

Actions(driver).click(myWebElement).build().perform();

click方法和action类都属于webdriver.Action类用于模拟复杂的用户手势(包括诸如拖放或使用Control键等单击多个元素之类的动作).click方法用于单击相应的webElement (按钮,链接等).SeleniumWebdriver使用浏览器的本机支持,使用id/xpath等定位符将DOM元素映射到WebElement对象.

Both click method and actions class belong to webdriver.Action class is used for emulating complex user gestures(including actions such as Drag and Drop or clicking multiple elements With Control key etc).click method is used to click on the respective webElement(buttons,links etc).Selenium Webdriver uses browser's native support for mapping the DOM element to WebElement object using locators like id/xpath etc.

JavaScriptExecutor是一个接口,提供通过硒驱动程序执行Javascript的机制.它提供了执行脚本"和""executeAsyncScript"方法,用于在当前选定的框架或窗口的上下文中运行外部JavaScript.对于executescript,它将返回一个DOM元素,然后将其转换为WebElement

JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver. It provides "executescript" & "executeAsyncScript" methods, to run external JavaScript in the context of the currently selected frame or window.In the case of executescript it will return an DOM element which is then converted to WebElement

与使用JavaScript调用的用户相比,WebDriver在浏览器上模拟的点击与实际用户的操作类似.

The click simulated by WebDriver on a browser is similar to what actual user do as compared to one invoked using javascript

示例场景:

<html>
<body>
<button type = "button" id ="test" style = "display:none"> clickme </button>
</body>
</html>

如果使用webdriver中的单击功能单击单击我"按钮,您将得到一个org.openqa.selenium.ElementNotVisibleException(元素不可见异常),它是正确的,因为该元素存在于DOM中,但不会以如下形式显示给用户设置了css样式display:none

If you click on the "click me" button using click function in webdriver you will get an org.openqa.selenium.ElementNotVisibleException (Element not visible exception) which is correct as the element is present in the DOM but is not displayed to the user as the css style display:none is set

((JavascriptExecutor)driver).executeScript("$('#test').click();");//or
((JavascriptExecutor)driver).executeScript("document.getElementById('test').click();");

如果您使用上述javascript/jquery单击该元素,则无论按钮是否可见,它都会单击该按钮,这是错误的,因为最终用户将无法看到/单击该按钮但您的脚本会通过.因此,最合理的做法是尝试尽可能使用网络驱动程序功能,而不是使用JavaScript

If you use the above javascript/jquery to click on the element then it will click on the button regardless of whether the button was visible or not which is wrong because the end user will not be able to see/click on the button but your script will pass.So the moral is try to use webdriver functions wherever possible instead of using javascript

希望这对您有所帮助.如有任何疑问,请尽快回来

Hope this helps you.Kindly get back if you have any queries

这篇关于单击使用javascript vs action vs webdriver的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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