Find Multiple Elements using Selenide script

Tutorial 3​​ ​​ Find multiple elements using​​ Selenide​​ Script

What you will Learn:

  • Usage of​​ $$​​ to find multiple elements

  • Code snippet

Usage of $$ to find multiple elements

In​​ the previous blog, we had executed a selenide script that would search ‘way2automation’ text. As seen below, there are multiple results on the page

Inspect any link as shown below

Notice above that there are 7 links that have the same classname. In other words,​​ there are multiple elements​​ on the page​​ having the same classname.​​ 

We can use $$ to find multiple elements

So we can write like this

Let us fetch the count of multiple links present on the page having same css selector

The code snippet

Save and execute, the count is printed

We can add an assertion as well

Save and execute, the test passes

Let us try to fail the assertion by writing 17​​ (or any other number you wish to)​​ instead of 7

Save and execute, the test fails as expected

 

This is how we can use​​ $$ method to find an element.

Code snippet

package​​ w2a.selenide;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.Assert;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.CollectionCondition;

import​​ com.codeborne.selenide.Condition;

import​​ static​​ com.codeborne.selenide.Selenide.*;

import​​ static​​ com.codeborne.selenide.Condition.*;

 

public​​ class​​ FirstSelenideTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException {

open("https://www.google.com/");

$(By.cssSelector(".gLFyf")).setValue("way2automation");

$(By.cssSelector(".gNO89b")).click();

$(By.cssSelector(".LC20lb.MBeuO.DKV0Md")).shouldHave(Condition.appear);

 String​​ actual_text​​ =​​ $(By.cssSelector(".LC20lb.MBeuO.DKV0Md")).getText();

 Assert.assertEquals(actual_text,​​ "Way2Automation: Get Online Selenium Certification Course");

int​​ w2a_count​​ =​​ $$(By.cssSelector(".LC20lb.MBeuO.DKV0Md")).size();

 System.out.println("The count is -->"​​ +​​ w2a_count);

//Assertion

$$(By.cssSelector(".LC20lb.MBeuO.DKV0Md")).shouldHave(CollectionCondition.size(17));

 Thread.sleep(1000);

 }

}


Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024