简体   繁体   中英

Understanding the Method findElement in Selenium

I am trying to understand the different interfaces, class implementing the interface and methods in Selenium.

I could understand that Interface SearchContext is inherited by interface WebDriver and which in turn is implemented by different classes like ForefoxDriver and others.

findElement is a method as part of SearchContext interface and which is implemented by the FirefoxDriver (since fireFoxDriver implements WebDriver).

There is another class called "By" which has set of nested sub classes.

Now the syntax for findElement is like:

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

I couldn't understand the parameter that is passed into the method findElement, because is it an object that is passed as parameter or is some other function is called inside findElement method?

Could any anyone clarify what exactly is the parameter passed into this function findElement?

Thank you.

(Java) According to the 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() takes one argument. An object of the By class. Let's take a look at the By class.

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

In short, these are ALL ways that you are able find the elemnts that you need. It all depends on your philosophies on which you select on. I personally always use By.cssSelector .

This is selenium's way of selecting elements, similar to fluent api. It makes readability alot easier for users. The parameter you pass into findElement is kind of like a pseudo-selector query, similar to Jsoup.

For example, if you wanted to select the SO logo at the top left hand corner of this page, you could do

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

So the query By.name("q") basically selects element(s) with the attribute, name="q"

While parsing a DOM object, you can get an element using various ways

  • you can get an element by id
  • You can get an element by its name
  • You can get an element by its class

So there are various ways to get an element

In selenium, while you are using findElement method, you will have to say "using which" you are instructing selenium to find the element ? using name or using id or watever it is ..

Thats why they have given a class By. So if you wanna find an element with class name, then you can use findElement(By.ByClassName)

If you wanna find the element by Id, then you can use findElement(By.ById)

see this api link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM