element(...).getWebElement()
over element(...)
when both works exactly the samegetWebElement() would result into a WebElement instance from WebDriverJS . Basically, this gives you access to the pure "bare-metal" WebElement .
In protractor, the single web element belongs to type ElementFinder. The ElementFinder can be treated as a WebElement for most purposes, in particular, you may perform actions (i.e. click, getText) on them as you would a WebElement.
ElementFinder can be used to build a chain of locators that is used to find an element. An ElementFinder does not actually attempt to find the element until an action is called, which means they can be set up in helper files before the page is available.
import { browser, element, by} from 'protractor' import { protractor } from 'protractor/built/ptor'; describe('Protractor Typescript Demo', function() { browser. ignoreSynchronization = true; it('click operation', function() { browser. get('https://google.com/'); browser. sleep(1000) element(by.name('q')).
GetWebElement() function in Protractor. Purpose: The element() returns value type of ElementFinder. However, if required we can use the getWebElement() function to get the element of type WebElement. Syntax: element(locator).getWebElement(): WebElementPromise; Returns: This function returns the value of type WebElementPromise. Code Example:
When we use element (locator)) the protractor returns element of type ElementFinder, as discussed earlier ElementFinder is a protractor wrapper. There might be some instance where one may need pure or raw web element which selenium returns.
Note: ElementFinder can be chained with element array finder which will be covered in the next article. Purpose: The element () returns value type of ElementFinder. However, if required we can use the getWebElement () function to get the element of type WebElement. What is the difference between Element (locator) and GetWebElement ()?
With the help of protractor and selenium, these elements are uniquely identified. Each web element is associated with the property such as class, tags, id or attributes which uniquely identifies that element. What is ElementFinder in Protractor? In protractor, the single web element belongs to type ElementFinder.
Protractor
is a convenient wrapper around WebDriverJS
- javascript selenium bindings.
element(...)
would result into an ElementFinder
instance introduced in Protractorelement(...).getWebElement()
would result into a WebElement
instance from WebDriverJS
. Basically, this gives you access to the pure "bare-metal" WebElement
.The most common use-case for using getWebElement()
is when you need to pass an ElementFinder
as a script argument - currently you have to call getWebElement()
for this to work:
var elm = element(by.id("myid"));
browser.executeScript("arguments[0].click()", elm.getWebElement());
There is an open feature-request to be able to pass ElementFinder
directly:
browser.executeScript("arguments[0].click()", elm); // not gonna work as of now
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With