|
1 | 1 | ---
|
2 |
| -title: "Interacting with web elements" |
3 |
| -linkTitle: "Interactions" |
| 2 | +title: "Web元素交互" |
| 3 | +linkTitle: "交互" |
4 | 4 | weight: 2
|
5 | 5 | description: >
|
6 |
| - A high-level instruction set for manipulating form controls. |
7 |
| -needsTranslation: true |
| 6 | + 用于操纵表单的高级指令集. |
| 7 | +
|
8 | 8 | ---
|
9 | 9 |
|
10 |
| -There are only 5 basic commands that can be executed on an element: |
11 |
| -* [click](https://w3c.github.io/webdriver/#element-click) (applies to any element) |
12 |
| -* [send keys](https://w3c.github.io/webdriver/#element-send-keys) (only applies to text fields and content editable elements) |
13 |
| -* [clear](https://w3c.github.io/webdriver/#element-send-keys) (only applies to text fields and content editable elements) |
14 |
| -* submit (only applies to form elements) |
15 |
| -* select (see [Select List Elements](select_lists.md)) |
16 |
| - |
17 |
| -## Additional validations |
18 |
| - |
19 |
| -These methods are designed to closely emulate a user's experience, so, |
20 |
| -unlike the [Actions API]({{< ref "/documentation/webdriver/actions_api/" >}}), it attempts to perform two things |
21 |
| -before attempting the specified action. |
22 |
| -1. If it determines the element is outside the viewport, it |
23 |
| - [scrolls the element into view](https://w3c.github.io/webdriver/#dfn-scrolls-into-view), specifically |
24 |
| - it will align the bottom of the element with the bottom of the viewport. |
25 |
| -2. It ensures the element is [interactable](https://w3c.github.io/webdriver/#interactability) |
26 |
| - before taking the action. This could mean that the scrolling was unsuccessful, or that the |
27 |
| - element is not otherwise displayed. Determining if an element is displayed on a page was too difficult to |
28 |
| - [define directly in the webdriver specification](https://w3c.github.io/webdriver/#element-displayedness), |
29 |
| - so Selenium sends an execute command with a JavaScript atom that checks for things that would keep |
30 |
| - the element from being displayed. If it determines an element is not in the viewport, not displayed, not |
31 |
| - [keyboard-interactable](https://w3c.github.io/webdriver/#dfn-keyboard-interactable), or not |
32 |
| - [pointer-interactable](https://w3c.github.io/webdriver/#dfn-pointer-interactable), |
33 |
| - it returns an [element not interactable](https://w3c.github.io/webdriver/#dfn-element-not-interactable) error. |
34 |
| - |
35 |
| -## Click |
36 |
| - |
37 |
| -The [element click command](https://w3c.github.io/webdriver/#dfn-element-click) is executed on |
38 |
| -the [center of the element](https://w3c.github.io/webdriver/#dfn-center-point). |
39 |
| -If the center of the element is [obscured](https://w3c.github.io/webdriver/#dfn-obscuring) for some reason, |
40 |
| -Selenium will return an [element click intercepted](https://w3c.github.io/webdriver/#dfn-element-click-intercepted) error. |
41 |
| - |
42 |
| -## Send keys |
43 |
| - |
44 |
| -The [element send keys command](https://w3c.github.io/webdriver/#dfn-element-send-keys) |
45 |
| -types the provided keys into an [editable](https://w3c.github.io/webdriver/#dfn-editable) element. |
46 |
| -Typically, this means an element is an input element of a form with a `text` type or an element |
47 |
| -with a`content-editable` attribute. If it is not editable, |
48 |
| -[an invalid element state](https://w3c.github.io/webdriver/#dfn-invalid-element-state) error is returned. |
49 |
| - |
50 |
| -[Here](https://www.w3.org/TR/webdriver/#keyboard-actions) is the list of |
51 |
| -possible keystrokes that WebDriver Supports. |
| 10 | +仅有五种基本命令可用于元素的操作: |
| 11 | +* [点击](https://w3c.github.io/webdriver/#element-click) (适用于任何元素) |
| 12 | +* [发送键位](https://w3c.github.io/webdriver/#element-send-keys) (仅适用于文本字段和内容可编辑元素) |
| 13 | +* [清除](https://w3c.github.io/webdriver/#element-send-keys) (仅适用于文本字段和内容可编辑元素) |
| 14 | +* 提交 (仅适用于表单元素) |
| 15 | +* 选择 (参见 [选择列表元素](select_lists.md)) |
| 16 | + |
| 17 | +## 附加验证 |
| 18 | + |
| 19 | +这些方法的设计目的是尽量模拟用户体验, 所以, |
| 20 | +与 [Actions接口]({{< ref "/documentation/webdriver/actions_api/" >}}) 不同, |
| 21 | +在指定制定操作之前, |
| 22 | +会尝试执行两件事. |
| 23 | +1. 如果它确定元素在视口之外, |
| 24 | + [则会将元素滚动到视图中](https://w3c.github.io/webdriver/#dfn-scrolls-into-view), |
| 25 | + 特别是将元素底部与视口底部对齐. |
| 26 | +2. 确保元素在执行操作之前是[可交互的](https://w3c.github.io/webdriver/#interactability) . |
| 27 | + 这可能意味着滚动不成功, |
| 28 | + 或者该元素没有以其他方式显示. |
| 29 | + 确定某个元素是否显示在页面上太难了 |
| 30 | + [无法直接在webdriver规范中定义](https://w3c.github.io/webdriver/#element-displayedness), |
| 31 | + 因此Selenium发送一个带有JavaScript原子的执行命令, |
| 32 | + 检查是否有可能阻止该元素显示. |
| 33 | + 如果确定某个元素不在视口中, 不显示, 不可 |
| 34 | + [键盘交互](https://w3c.github.io/webdriver/#dfn-keyboard-interactable), |
| 35 | + 或不可 |
| 36 | + [指针交互](https://w3c.github.io/webdriver/#dfn-pointer-interactable), |
| 37 | + 则返回一个[元素不可交互](https://w3c.github.io/webdriver/#dfn-element-not-interactable) 错误. |
| 38 | + |
| 39 | +## 点击 |
| 40 | + |
| 41 | +[元素点击命令](https://w3c.github.io/webdriver/#dfn-element-click) 执行在 [元素中央](https://w3c.github.io/webdriver/#dfn-center-point). |
| 42 | +如果元素中央由于某些原因被 [遮挡](https://w3c.github.io/webdriver/#dfn-obscuring) , |
| 43 | +Selenium将返回一个 [元素点击中断](https://w3c.github.io/webdriver/#dfn-element-click-intercepted) 错误. |
| 44 | + |
| 45 | +## 发送键位 |
| 46 | + |
| 47 | +[元素发送键位命令](https://w3c.github.io/webdriver/#dfn-element-send-keys) |
| 48 | +将录入提供的键位到 [可编辑的](https://w3c.github.io/webdriver/#dfn-editable) 元素. |
| 49 | +通常, 这意味着元素是具有 `文本` 类型的表单的输入元素或具有 `内容可编辑` 属性的元素. |
| 50 | +如果不可编辑, 则返回 |
| 51 | +[无效元素状态](https://w3c.github.io/webdriver/#dfn-invalid-element-state) 错误. |
| 52 | + |
| 53 | +[以下](https://www.w3.org/TR/webdriver/#keyboard-actions) |
| 54 | +是WebDriver支持的按键列表. |
52 | 55 |
|
53 | 56 | {{< tabpane >}}
|
54 | 57 | {{< tab header="Java" >}}
|
@@ -147,14 +150,15 @@ driver.quit()
|
147 | 150 | {{< /tab >}}
|
148 | 151 | {{< /tabpane >}}
|
149 | 152 |
|
150 |
| -## Clear |
| 153 | +## 清除 |
151 | 154 |
|
152 |
| -The [element clear command](https://w3c.github.io/webdriver/#dfn-element-clear) resets the content of an element. |
153 |
| -This requires an element to be [editable](https://w3c.github.io/webdriver/#dfn-editable), |
154 |
| -and [resettable](https://w3c.github.io/webdriver/#dfn-resettable-elements). Typically, |
155 |
| -this means an element is an input element of a form with a `text` type or an element |
156 |
| -with a`content-editable` attribute. If these conditions are not met, |
157 |
| -[an invalid element state](https://w3c.github.io/webdriver/#dfn-invalid-element-state) error is returned. |
| 155 | +[元素清除命令](https://w3c.github.io/webdriver/#dfn-element-clear) |
| 156 | +重置元素的内容. |
| 157 | +这要求元素 [可编辑](https://w3c.github.io/webdriver/#dfn-editable), |
| 158 | +且 [可重置](https://w3c.github.io/webdriver/#dfn-resettable-elements). |
| 159 | +通常, 这意味着元素是具有 `文本` 类型的表单的输入元素或具有 `内容可编辑` 属性的元素. |
| 160 | +如果不满足这些条件, 将返回 |
| 161 | +[无效元素状态](https://w3c.github.io/webdriver/#dfn-invalid-element-state) 错误. |
158 | 162 |
|
159 | 163 | {{< tabpane langEqualsHeader=true >}}
|
160 | 164 | {{< tab header="Java" >}}
|
@@ -276,9 +280,11 @@ driver.quit()
|
276 | 280 | {{< /tab >}}
|
277 | 281 | {{< /tabpane >}}
|
278 | 282 |
|
279 |
| -## Submit |
| 283 | +## 提交 |
280 | 284 |
|
281 |
| -In Selenium 4 this is no longer implemented with a separate endpoint and functions by executing a script. As |
282 |
| -such, it is recommended not to use this method and to click the applicable form submission button instead. |
| 285 | +在Selenium 4中, |
| 286 | +不再通过单独的端点以及脚本执行的方法来实现. |
| 287 | +因此, 建议不要使用此方法, |
| 288 | +而是单击相应的表单提交按钮. |
283 | 289 |
|
284 | 290 |
|
0 commit comments