@@ -11,10 +11,9 @@ description: >
1111 根据提供的定位值定位元素.
1212---
1313
14- One of the most fundamental aspects of using Selenium is obtaining element references to work with.
15- Selenium offers a number of built-in [ locator strategies] ({{< ref "locators.md" >}}) to uniquely identify an element.
16- There are many ways to use the locators in very advanced scenarios. For the purposes of this documentation,
17- let's consider this HTML snippet:
14+ 使用Selenium的一个最基本的方面是通过元素查询器获得元素引用。
15+ Selenium提供了许多内置的[ 定位器策略] ({{< ref "locators.md" >}})来唯一标识元素。
16+ 有很多方法可以在非常高级的场景中使用定位器。作为样例,让我们来看看这个HTML代码段:
1817
1918
2019``` html
@@ -30,17 +29,15 @@ let's consider this HTML snippet:
3029</ul >
3130```
3231
33- ## First matching element
32+ ## 第一个匹配条件的元素
3433
35- Many locators will match multiple elements on the page. The singular find element method will return a reference to the
36- first element found within a given context.
34+ 同一个页面上可能有多个元素符合定位器匹配条件。单次` find element ` 方法调用将只会返回一个指向在给定上下文中找到的第一个元素的引用。
3735
38- ### Evaluating entire DOM
3936
40- When the find element method is called on the driver instance, it
41- returns a reference to the first element in the DOM that matches with the provided locator.
42- This value can be stored and used for future element actions. In our example HTML above, there are
43- two elements that have a class name of "tomatoes" so this method will return the element in the "vegetables" list.
37+ ### 在整个DOM中查找元素
38+
39+ 当在驱动实例上调用 ` find element` 方法时,它会返回DOM中与所提供的定位器匹配的第一个元素的引用。这个引用可以被保存用于之后对此元素的操作。
40+ 在上面的HTML示例中,有两个元素的类名都是 "tomatoes",他们分别在两个列表中。以下代码将返回第一个列表,即 "vegetables "列表中的"tomatos"元素引用。
4441
4542{{< tabpane langEqualsHeader=true >}}
4643 {{< tab header="Java" >}}
@@ -64,14 +61,12 @@ val vegetable: WebElement = driver.findElement(By.className("tomatoes"))
6461{{< /tabpane >}}
6562
6663
67- ### Evaluating a subset of the DOM
64+ ### 在部分DOM中查找元素
65+
66+ 以上代码无法获取在样例第二个列表中的"tomato"元素。如果知道目标元素所属位置且希望获取指定范围里的元素而不是整个上下文中的第一个,可以进行部分搜索。
6867
69- Rather than finding a unique locator in the entire DOM, it is often useful to narrow the search to the scope
70- of another located element. In the above example there are two elements with a class name of "tomatoes" and
71- it is a little more challenging to get the reference for the second one.
7268
73- One solution is to locate an element with a unique attribute that is an ancestor of the desired element and not an
74- ancestor of the undesired element, then call find element on that object:
69+ 一种方案是先查找两个目标同名元素的不同名父节点,从而获取两个元素所属的不同部分的父节点元素。查找的父节点应该各自拥有唯一属性,且这个父节点不能是目标元素本身。在此之上,再次对得到的父节点运行` find element ` 查找目标元素。
7570
7671{{< tabpane langEqualsHeader=true >}}
7772 {{< tab header="Java" >}}
@@ -101,23 +96,19 @@ val fruit = fruits.findElement(By.className("tomatoes"))
10196{{< /tabpane >}}
10297
10398{{% pageinfo color="info" %}}
104- ** Java and C#** <br >
105- ` WebDriver ` , ` WebElement ` and ` ShadowRoot ` classes all implement a ` SearchContext ` interface, which is
106- considered a _ role-based interface_ . Role-based interfaces allow you to determine whether a particular
107- driver implementation supports a given feature. These interfaces are clearly defined and try
108- to adhere to having only a single role of responsibility.
99+ ** Java 和 C#** <br >
100+ ` WebDriver ` , ` WebElement ` 和 ` ShadowRoot ` 三个类都实现了 ` SearchContext ` 接口。这个接口是 _ 基于角色的接口_ 。
101+
102+ 基于角色的接口允许您确定特定驱动程序实现是否支持给定功能。这些接口明确定义了所属角色的信息,每个对象尽量遵守只负责一个角色。
109103{{% /pageinfo %}}
110104
111- ### Optimized locator
105+ ### 定位器优化
112106
113- A nested lookup might not be the most effective location strategy since it requires two
114- separate commands to be issued to the browser.
107+ 上述嵌套查找会导致客户端向驱动发送多条不同的请求。这可能导致效率问题。
115108
116- To improve the performance slightly, we can use either CSS or XPath to find this element in a single command.
117- See the [ Locator strategy suggestions] ({{< ref "/documentation/test_practices/encouraged/locators" >}}) in our
118- [ Encouraged test practices] ({{< ref "/documentation/test_practices/encouraged" >}}) section.
109+ 为了略微提高性能,我们可以使用 CSS 或 XPath 在单个命令中查找该元素。请查看在[ 最佳实践] ({{< ref "/documentation/test_practices/encouraged" >}})中的[ 使用定位器的提示] ({{< ref "/documentation/test_practices/encouraged/locators" >}})部分。
119110
120- For this example, we'll use a CSS Selector:
111+ 如下所示,我们通过CSS路径定位器查找元素:
121112
122113{{< tabpane langEqualsHeader=true >}}
123114 {{< tab header="Java" >}}
@@ -141,12 +132,11 @@ val fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"))
141132{{< /tabpane >}}
142133
143134
144- ## All matching elements
135+ ## 获取所有符合条件元素的列表
136+
137+ 某些情况下需要获取当前查询DOM下所有符合定位器条件的元素的引用,而不是只有第一个。查找多个元素(` findElements ` )方法返回一个元素引用列表。如果没有匹配的元素,则返回空列表。
145138
146- There are several use cases for needing to get references to all elements that match a locator, rather
147- than just the first one. The plural find elements methods return a collection of element references.
148- If there are no matches, an empty list is returned. In this case,
149- references to all fruits and vegetable list items will be returned in a collection.
139+ 在以下代码中,所有水果和蔬菜列表内项目元素的引用将以集合的形式返回。
150140
151141{{< tabpane langEqualsHeader=true >}}
152142 {{< tab header="Java" >}}
@@ -169,9 +159,8 @@ val plants: List<WebElement> = driver.findElements(By.tagName("li"))
169159 {{< /tab >}}
170160{{< /tabpane >}}
171161
172- ### Get element
173- Often you get a collection of elements but want to work with a specific element, which means you
174- need to iterate over the collection and identify the one you want.
162+ ### 从元素集合中获取元素
163+ 你可以通过` findElements ` 获取元素集合后处理所有元素或者其中一个元素。由于返回的是一个集合,因此你可以通过遍历从而逐个处理或者筛选目标元素。
175164
176165
177166{{< tabpane langEqualsHeader=true >}}
@@ -279,10 +268,9 @@ fun main() {
279268 {{< /tab >}}
280269{{< /tabpane >}}
281270
282- ## Find Elements From Element
271+ ## 由父节点查找子节点元素
283272
284- It is used to find the list of matching child WebElements within the context of parent element.
285- To achieve this, the parent WebElement is chained with 'findElements' to access child elements
273+ 类` WebElement ` 本身支持` findElements ` 函数。当需要查找一个父节点之下的某个元素,且拥有父节点元素引用时,可以通过对父节点执行查找获取目标元素(或元素列表)。
286274
287275{{< tabpane langEqualsHeader=true >}}
288276 {{< tab header="Java" >}}
@@ -418,9 +406,9 @@ namespace FindElementsFromElement {
418406 {{< /tab >}}
419407{{< /tabpane >}}
420408
421- ## Get Active Element
409+ ## 获取当前活动(得到焦点)的元素
422410
423- It is used to track (or) find DOM element which has the focus in the current browsing context.
411+ 用于查找(或跟踪)当前上下文中拥有焦点的元素。
424412
425413{{< tabpane langEqualsHeader=true >}}
426414 {{< tab header="Java" >}}
0 commit comments