【问题标题】:Understanding the Method findElement in Selenium了解 Selenium 中的方法 findElement
【发布时间】:2013-11-21 18:44:34
【问题描述】:

我试图了解 Selenium 中不同的接口、实现接口的类和方法。

我可以理解接口 SearchContext 是由接口 WebDriver 继承的,而后者又由不同的类(如 ForefoxDriver 等)实现。

findElement 是 SearchContext 接口的一部分,由 FirefoxDriver 实现(因为 fireFoxDriver 实现了 WebDriver)。

还有一个名为“By”的类,它有一组嵌套的子类。

现在 findElement 的语法如下:

driver.findElement(By.name("q"));

我无法理解传递给方法 findElement 的参数,因为它是作为参数传递的对象还是在 findElement 方法中调用了其他函数?

谁能澄清传递给这个函数 findElement 的参数到底是什么?

谢谢。

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    (Java) 根据 Selenium 2 API,

    #findElement():
    
      Find the first WebElement using the given method. This method is affected by the 'implicit wait' times in force at the time of execution. The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached. findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
      Specified by: findElement(...) in SearchContext
    
      Parameters:
        by The locating mechanism
    
      Returns:
        The first matching element on the current page
    
      Throws:
        NoSuchElementException - If no matching elements are found
    
      See Also:
        org.openqa.selenium.By
        org.openqa.selenium.WebDriver.Timeouts
    

    findElement() 接受一个参数。 By 类的对象。我们来看看By这个类。

    By.className       : Finds elements based on the value of the "class" attribute.
    By.cssSelector     : Finds elements via the driver's underlying W3 Selector engine.
    By.id              : a By which locates elements by the value of the "id" attribute.
    By.linkText        : a By which locates A elements by the exact text it displays
    By.name            : a By which locates elements by the value of the "name" attribute.
    By.partialLinkText : a By which locates A elements that contain the given link text
    By.tagName         : a By which locates elements by their tag name
    By.xpath           : a By which locates elements via XPath
    

    简而言之,这些都是您能够找到所需元素的所有方法。这完全取决于您选择的理念。我个人一直使用By.cssSelector

    【讨论】:

      【解决方案2】:

      这是 selenium 选择元素的方式,类似于 fluent api。它使用户更容易阅读。传入 findElement 的参数有点像伪选择器查询,类似于 Jsoup。

      例如,如果你想选择页面左上角的 SO 标志,你可以这样做

      driver.findElement(By.id("hlogo"));
      

      所以查询By.name("q") 基本上选择具有name="q" 属性的元素

      【讨论】:

      • 谢谢,我刚刚在 Fluent api 上搜索了一下,它谈到了方法级联。因此这里的 By.id("hogo") 是一个嵌套方法,其中“id”是方法名,“hogo”是传递的参数。但是根据 selenium 的文档,“By”是一个抽象类。因此我们可以用抽象类调用静态方法吗?
      • Selenium 不使用方法级联,所以我们不能真正按照 wiki 的定义将它归类为 fluent api。关于您的问题,抽象类基本上是具有某些实现的接口。因此它可以包含静态方法。重申By 的文档所指定的内容:它是一种用于在文档中定位元素的机制。对于想要提供自己的实现的用户来说,它是抽象的。
      • 如果你看一下[findElement's documentation](selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/…,它需要一个By对象作为它的参数。By的静态方法id(String attrValue)或``name(String attrValue)` 返回一个By 对象,该对象被输入findElement()
      • @Michael 它确实使用了级联。很有可能,您使用变量来引用返回值,因此它可能看起来不流畅。不过driver.findElement(...).findElement(...).findElements(...).get(0).findElement(...)绝对是流利的。
      【解决方案3】:

      在解析 DOM 对象时,可以通过多种方式获取元素

      • 可以通过id获取元素
      • 您可以通过名称获取元素
      • 您可以通过其类获取元素

      所以有多种获取元素的方法

      在 selenium 中,当您使用 findElement 方法时,您必须说 “使用哪个” 您正在指示 selenium 查找元素?使用名称或使用 id 或 waterever 它是..

      这就是他们给 By 上课的原因。所以如果你想找到一个类名的元素,那么你可以使用findElement(By.ByClassName)

      如果你想通过Id查找元素,那么你可以使用findElement(By.ById)

      this api link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2018-10-05
        • 2016-09-14
        • 1970-01-01
        • 2018-06-03
        相关资源
        最近更新 更多