Selenium的ByChained类实际上是如何工作的? [英] How Selenium's ByChained class really works?

查看:157
本文介绍了Selenium的ByChained类实际上是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.它说:

I am very confused with what the documentation for ByChained class mentions. It says:

一种机制,该机制用于使用一系列其他查找来定位文档中的元素.此类将找到与之匹配的所有DOM元素 每个定位器按顺序排列,例如driver.findElements(新 ByChained(by1,by2))将查找所有与by2和 出现在与by1匹配的元素下.

Mechanism used to locate elements within a document using a series of other lookups. This class will find all DOM elements that matches each of the locators in sequence, e.g. driver.findElements(new ByChained(by1, by2)) will find all elements that match by2 and appear under an element that matches by1.

还有关于硒的问题 code.google.com,是针对ByChained类而提出的,有人在评论中说,它旨在用于使用多个定位符查找元素/元素.

我不明白为什么by1和by2是两个不同元素的定位符?当我初次接触该类时,我觉得它将通过使用不同的定位器来帮助定位元素.因此,如果一个定位器不起作用,则下一个定位器将起作用.但是,当我实际使用此类时,它的行为非常奇怪,并始终抛出NoSuchElementException.

There is also an issue for selenium on code.google.com, raised for ByChained class where someone has commented saying it's meant to be used to find element/elements using several locators.

I don't get it. Why should by1 and by2 be locators of two different elements? When I initally came across this class I felt that it will help in locating element(s) by using different locators. So that if one locator does not work, the next would work. But when I practically used this class it behaved very weirdly and threw NoSuchElementException all the time.

例如,如果我的html是:

For example, if my html is:

<html>
  <body>
    <div id="details">
      <input id="firstName" class="personName" type="text"/>
    </div>
  </body>
</html>

我想通过在ByChained中使用两个定位符来查找输入字段:
1.使用By.id("firstName")
2.使用By.className("personName")

I want to find the input field by using two locators in ByChained:
1. using By.id("firstName")
2. using By.className("personName")

所以我的代码变为:

So my code becomes:

By myBy = new ByChained(By.id("firstName"),By.className("personName"));
driver.findElement(myBy);

执行时,出现NoSuchElementException.我期望如果我的第一个By不起作用,那么它将在系列中找到带有下一个By的元素.

On execution, I got NoSuchElementException. I was expecting that if my first By did not work, then it will find the element with the next By in the series.

有人可以通过一个示例解释该类的工作原理以及可以在哪种情况下使用它吗?

Can someone please explain how this class works with an example and in which scenarios it could be used?

推荐答案

此类的作用是使您可以使用dom中的层次结构来查找元素.

What this class does is allow you to locate an element using its heirarchy in the dom.

出于某些原因,请说您拥有以下html:

lets say for some reason you have the following html:

<html>
    <body>
        <div id="details">
            <input id="firstName" class="personName" type="text"/>
        </div>
        <input id="firstName" class="personName" type="text"/>
    </body>
</html>

,并且您想要获取位于div之间的元素,而不是单独获取的元素.您可以使用ByChained by通过执行以下操作来指定所需的元素:

and you want to get the element that is between the div rather than the one on its own. You can use the ByChained by to specify that you want that element by doing the following:

new ByChained(By.id("details"),By.id("firstName"));

发生的事情是,它找到第一个元素,然后在dom层次结构中的下一个元素中搜索列表中的下一个选择器.基本上,此By只是一种不再需要执行以下操作的好方法:

What happens is that it finds the first element then searches underneath that in the dom heirarchy for the selector that comes next in the list. Basically this By is just a nice clean way of no longer having to do the following:

details = driver.findElement(By.id("details"));
input = details.findElement(By.id("firstName"));

这篇关于Selenium的ByChained类实际上是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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