使用 javascript、动作还是 webdriver 单击元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32045218/
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
Clicking an element using javascript vs actions vs webdriver?
提问by Ragunath Chilkuru
We can click web-element using the following methods.
我们可以使用以下方法单击 web-element。
myWebElement.click();
or
或者
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", myWebElement);
or
或者
Actions(driver).click(myWebElement).build().perform();
What is the difference in these methods?
这些方法有什么区别?
回答by Vicky
myWebElement.click();
Actions(driver).click(myWebElement).build().perform();
myWebElement.click();
操作(驱动程序)。单击(myWebElement)。构建()。执行();
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.
click 方法和actions 类都属于webdriver。Action 类用于模拟复杂的用户手势(包括拖放等动作或使用Control 键单击多个元素等)。click 方法用于单击相应的webElement(按钮、链接等)。Selenium Webdriver 使用浏览器的本机支持,使用 id/xpath 等定位器将 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
JavaScriptExecutor 是一个接口,它提供了通过 selenium 驱动程序执行 Javascript 的机制。它提供了“executescript”和“executeAsyncScript”方法,以在当前选择的框架或窗口的上下文中运行外部JavaScript。在executescript的情况下,它将返回一个DOM元素,然后将其转换为WebElement
The click simulated by WebDriver on a browser is similar to what actual user do as compared to one invoked using javascript
与使用 javascript 调用的点击相比,WebDriver 在浏览器上模拟的点击与实际用户所做的相似
Example scenario:
示例场景:
<html>
<body>
<button type = "button" id ="test" style = "display:none"> clickme </button>
</body>
</html>
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
如果您在 webdriver 中使用单击功能单击“单击我”按钮,您将得到一个org.openqa.selenium.ElementNotVisibleException
(元素不可见异常),这是正确的,因为元素存在于 DOM 中,但在display:none
设置css 样式时不会向用户显示
((JavascriptExecutor)driver).executeScript("$('#test').click();");//or
((JavascriptExecutor)driver).executeScript("document.getElementById('test').click();");
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
如果您使用上面的 javascript/jquery 单击该元素,则无论该按钮是否可见,它都会单击该按钮,这是错误的,因为最终用户将无法看到/单击该按钮,但您的脚本会通过。所以道德是尽可能使用 webdriver 函数而不是使用 javascript
Hope this helps you.Kindly get back if you have any queries
希望这对您有所帮助。如果您有任何疑问,请回复