Jonathan
Jonathan

Reputation: 8891

What is the difference between a locator and a Webelement in Selenium?

In the documentation for expected_conditions in selenium the methods either take a webelement or take a locator. They are obviously quite similar, but what is the difference in them?

Upvotes: 3

Views: 7020

Answers (3)

Jonathan
Jonathan

Reputation: 8891

As mentioned by both of the other answerers. The locator is used to identify the actual element. Whereas the webelement is an object found. What does this look like in code?

The following gives you a webelement object. webelement = browser.find_element_by_id('id_here')

Whereas if you need to use a locator as described in the documentation you need to do the following. Example taken from Explicit waiting

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebdriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))

Upvotes: 1

Arran
Arran

Reputation: 25066

They are different.

A locator is nothing more than an abstract way of defining how an element will be found.

A WebElement is just the reference to that element within the DOM. It is Selenium's way of representing a DOM element to you so that you can manipulate it.

Upvotes: 1

Ajinkya
Ajinkya

Reputation: 22710

Locator is used to identify element. You can use it or if you have already created a element object you can use it.
If you pass element directly (if you have it) WebDriver willnot need to create a new object. If you pass locator WebDriver will use it to create Element object.
PS: I am not much familiar with Python.

Upvotes: 1

Related Questions