Selenium WebDriver“单击"和"Selenium WebDriver"之间有什么区别?和JavascriptExecutor请点击 [英] What is the difference between Selenium WebDriver "Click" and JavascriptExecutor Click

查看:138
本文介绍了Selenium WebDriver“单击"和"Selenium WebDriver"之间有什么区别?和JavascriptExecutor请点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面两个语句之间的执行过程差异.

Execution process difference between below two statements.

  1. driver.findElement(By.xpath("//input[@value='Save']")).click();
  2. ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
  1. driver.findElement(By.xpath("//input[@value='Save']")).click();
  2. ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

推荐答案

WebDriver click()模拟与UI的真实用户交互.我将通过向浏览器发送一个本机事件来执行(在大多数浏览器中),并且必须可见该事件才能单击它.从文档

WebDriver click() simulates real user interaction with the UI. I will be performed (in most browsers) by sending a native event to the browser, and it has to be visible in order to click on it. From the docs

...如果click()是通过发送本地事件(即 大多数浏览器/平台上默认)

...if click() is done by sending a native event (which is the default on most browsers/platforms)

点击某个元素有一些先决条件.元素 必须可见,并且高度和宽度必须大于0.

There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.

另一方面

JavaScript click()

在当前选定框架的上下文中执行JavaScript,或者 窗口.

Executes JavaScript in the context of the currently selected frame or window.

无论WebElement是否可见.这种方法错过了用户交互Selenium试图模拟的想法.

Regardless if the WebElement is visible or not. This approach misses the idea of user interaction Selenium tries to simulate.

这篇关于Selenium WebDriver“单击"和"Selenium WebDriver"之间有什么区别?和JavascriptExecutor请点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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