Locate Elements

Learn how to locate web elements with BELLATRIX web module.

Example

[Test]
public void PromotionsPageOpened_When_PromotionsButtonClicked()
{
    App.Navigation.Navigate("http://demos.bellatrix.solutions/");

    var promotionsLink = App.Components.CreateByLinkText<Anchor>("Promotions");

    promotionsLink.Click();
    Console.WriteLine(promotionsLink.By.Value);

    Console.WriteLine(promotionsLink.WrappedComponent.TagName);
}

Explanations

var promotionsLink = App.Components.CreateByLinkText<Anchor>("Promotions");

There are different ways to locate elements on the page. To do it you use the element create service. You need to know that BELLATRIX has a built-in complex mechanism for waiting for elements, so you do not need to worry about this anymore. Keep in mind that when you use the Create methods, the element is not searched on the page. All elements use lazy loading. Which means that they are searched once you perform an action or assertion on them. By default on each new action, the element is searched again and refreshed.

Console.WriteLine(promotionsLink.By.Value);

Because of the proxy element mechanism (we have a separate type of element instead of single WebDriver IWebElement interface) there are several benefits. Each control (element type; ComboBox, TextField and so on) contains only the actions you can do with it, and the methods are named properly. In vanilla WebDriver to type the text you call SendKeys method. Also, we have some additional properties in the proxy web control such as By. Now you can get the locator with which your element was found.

Console.WriteLine(promotionsLink.WrappedComponent.TagName);

You can access the WebDriver wrapped element through WrappedElement and the current WebDriver instance through WrappedDriver.

Available Create Methods

BELLATRIX extends the vanilla WebDriver selectors and give you additional ones.

CreateByIdEndingWith

App.Components.CreateByIdEndingWith<Anchor>("myIdSuffix");

Searches the element by ID ending with the locator.

CreateByTag

App.Components.CreateByTag<Anchor>("a");

Searches the element by its tag.

CreateById

App.Components.CreateById<Button>("myId");

Searches the element by its ID.

CreateByIdContaining

App.Components.CreateByIdContaining<Button>("myIdMiddle");

Searches the element by ID containing the specified text.

CreateByValueEndingWith

App.Components.CreateByIdContaining<Button>("pay");

Searches the element by value attribute containing the specified text.

CreateByXpath

App.Components.CreateByXpath<Button>("//*[@title='Add to cart']");

Searches the element by XPath locator.

CreateByLinkText

App.Components.CreateByLinkText<Anchor>("blog");

Searches the element by its link (href).

CreateByLinkTextContaining

App.Components.CreateByLinkTextContaining<Anchor>("account");

Searches the element by its link (href) if it contains specified value.

CreateByClass

App.Components.CreateByClassContaining<Anchor>("ul.products");

Searches the element by its CSS classes.

CreateByClassContaining

App.Components.CreateByClassContaining<Anchor>(".products");

Searches the element by its CSS classes containing the specified values.

CreateByInnerTextContaining

App.Components.CreateByInnerTextContaining<Div>("Showing all");

Searches the element by its inner text content, including all child HTML elements.

CreateByNameEndingWith

App.Components.CreateByNameEndingWith<Search>("a");

Searches the element by its name containing the specified text.

CreateByAttributesContaining

App.Components.CreateByAttributesContaining<Anchor>("data-product_id", "31");

Searches the element by some of its attribute containing the specified value.

Find Multiple Elements

Sometimes we need to find more than one component. For example, in this test we want to locate all Add to Cart buttons. To do it you can use the element create service CreateAll method.

[Test]
public void CheckAllAddToCartButtons()
{
    App.Navigation.Navigate("http://demos.bellatrix.solutions/");

    var blogLink = App.Components.CreateAllByXpath<Anchor>("//*[@title='Add to cart']");
}

Available CreateAll Methods

CreateAllByIdEndingWith

App.Components.CreateAllByIdEndingWith<Anchor>("myIdSuffix");

Searches the elements by ID ending with the locator.

CreateAllByTag

App.Components.CreateAllByTag<Anchor>("a");

Searches the elements by its tag.

CreateAllById

App.Components.CreateAllById<Button>("myId");

Searches the elements by its ID.

CreateAllByIdContaining

App.Components.CreateAllByIdContaining<Button>("myIdMiddle");

Searches the elements by ID containing the specified text.

CreateAllByValueEndingWith

App.Components.CreateAllByValueEndingWith<Button>("pay");

Searches the elements by value attribute containing the specified text.

CreateAllByXpath

App.Components.CreateAllByXpath<Button>("//*[@title='Add to cart']");

Searches the elements by XPath locator.

CreateAllByLinkText

App.Components.CreateAllByLinkText<Anchor>("blog");

Searches the elements by its link (href).

CreateAllByLinkTextContaining

App.Components.CreateAllByLinkTextContaining<Anchor>("account");

Searches the elements by its link (href) if it contains specified value.

CreateAllByClass

App.Components.CreateAllByClass<Anchor>("ul.products");

Searches the elements by its CSS classes.

CreateAllByClassContaining

App.Components.CreateAllByClassContaining<Anchor>(".products");

Searches the elements by its CSS classes containing the specified values.

CreateAllByInnerTextContaining

App.Components.CreateAllByInnerTextContaining<Div>("Showing all");

Searches the elements by its inner text content, including all child HTML elements.

CreateAllByNameEndingWith

App.Components.CreateAllByNameEndingWith<Search>("a");

Searches the elements by its name containing the specified text.

CreateAllByAttributesContaining

App.Components.CreateAllByAttributesContaining<Anchor>("data-product_id", "31");

Searches the elements by some of its attribute containing the specifed value.

Find Nested Elements

Sometimes it is easier to locate one element and then find the next one that you need, inside it. For example in this test we want to locate the Sale! button inside the product’s description. To do it you can use the element’s Create methods.

[Test]
public void OpenSalesPage_When_LocatedSaleButtonInsideProductImage()
{
    App.Navigation.Navigate("http://demos.bellatrix.solutions/");

    var productsColumn = App.Components.CreateByClass<Option>("products columns-4");

    var saleButton = productsColumn.CreateByClassContaining<Anchor>("woocommerce-LoopProduct-link woocommerce-loop-product__link").CreateByInnerTextContaining<Button>("Sale!");

    saleButton.Click();
}

The first products row is located. Then search inside it for the first product image, inside it search for the Sale! Span component.

Note: It is entirely legit to create a Button instead of Span. BELLATRIX library does not care about the real type of the HTML elements. The proxy types are convenience wrappers so to say. Meaning that they give you a better interface of predefined properties and methods to make your tests more readable.

Available Create Methods for Finding Nested Elements

CreateByIdEndingWith

component.CreateByIdEndingWith<Anchor>("myIdSuffix");

Searches the element by ID ending with the locator.

CreateByTag

component.CreateByTag<Anchor>("a");

Searches the element by its tag.

CreateById

component.CreateById<Button>("myId");

Searches the element by its ID.

CreateByIdContaining

component.CreateByIdContaining<Button>("myIdMiddle");

Searches the element by ID containing the specified text.

CreateByValueEndingWith

component.CreateByIdContaining<Button>("pay");

Searches the element by value attribute containing the specified text.

CreateByXpath

component.CreateByXpath<Button>("//*[@title='Add to cart']");

Searches the element by XPath locator.

CreateByLinkText

component.CreateByLinkText<Anchor>("blog");

Searches the element by its link (href).

CreateByLinkTextContaining

component.CreateByLinkTextContaining<Anchor>("account");

Searches the element by its link (href) if it contains specified value.

CreateByClass

component.CreateByClassContaining<Anchor>("ul.products");

Searches the element by its CSS classes.

CreateByClassContaining

component.CreateByClassContaining<Anchor>(".products");

Searches the element by its CSS classes containing the specified values.

CreateByInnerTextContaining

component.CreateByInnerTextContaining<Div>("Showing all");

Searches the element by its inner text content, including all child HTML elements.

CreateByNameEndingWith

component.CreateByNameEndingWith<Search>("a");

Searches the element by its name containing the specified text.

CreateByAttributesContaining

component.CreateByAttributesContaining<Anchor>("data-product_id", "31");

Available CreateAll Methods for Finding Nested Elements

CreateAllByIdEndingWith

component.CreateAllByIdEndingWith<Anchor>("myIdSuffix");

Searches the elements by ID ending with the locator.

CreateAllByTag

component.CreateAllByTag<Anchor>("a");

Searches the elements by its tag.

CreateAllById

component.CreateAllById<Button>("myId");

Searches the elements by its ID.

CreateAllByIdContaining

component.CreateAllByIdContaining<Button>("myIdMiddle");

Searches the elements by ID containing the specified text.

CreateAllByValueEndingWith

component.CreateAllByValueEndingWith<Button>("pay");

Searches the elements by value attribute containing the specified text.

CreateAllByXpath

component.CreateAllByXpath<Button>("//*[@title='Add to cart']");

Searches the elements by XPath locator.

CreateAllByLinkText

component.CreateAllByLinkText<Anchor>("blog");

Searches the elements by its link (href).

CreateAllByLinkTextContaining

component.CreateAllByLinkTextContaining<Anchor>("account");

Searches the elements by its link (href) if it contains specified value.

CreateAllByClass

component.CreateAllByClass<Anchor>("ul.products");

Searches the elements by its CSS classes.

CreateAllByClassContaining

component.CreateAllByClassContaining<Anchor>(".products");

Searches the elements by its CSS classes containing the specified values.

CreateAllByInnerTextContaining

component.CreateAllByInnerTextContaining<Div>("Showing all");

Searches the elements by its inner text content, including all child HTML elements.

CreateAllByNameEndingWith

component.CreateAllByNameEndingWith<Search>("a");

Searches the elements by its name containing the specified text.

CreateAllByAttributesContaining

component.CreateAllByAttributesContaining<Anchor>("data-product_id", "31");

Searches the elements by some of its attribute containing the specifed value.