我试图理解不同的接口,在Selenium中实现接口和方法的类。

我可以理解,接口SearchContext是由接口WebDriver继承的,而该接口又由不同的类(如ForefoxDriver和其他类)实现。

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

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

现在,findElement的语法如下:

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


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

任何人都可以澄清传递给此函数findElement的参数的确切含义吗?

谢谢。

最佳答案

(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

关于java - 了解 Selenium 中的方法findElement,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20129399/

10-10 19:58