Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Verify Text In Bold Using Selenium WebDriver

Posted on 12/11/2024 By admin

Recently, a guy asked me below question:

I need to verify title of an article is in bold. Article title has no or tag. How to verify it now?

It was also new to me and learnt new thing that day. So I am sharing my knowledge here.

HTML provides two tags to make text bold:- b and strong.

In above case you can easily locate the text and get tag name. You can assert tag name as “b” or “strong”.

This paragraph is made in bold using html 'b' tag.
This paragraph is made in bold using html 'strong' tag.

But CSS provides an attribute called “font-weight” which is more powerful. You can assign this to html element with values. You can make text normal, bold and bolder. Refer all possible values here.

If you have bold text using above css attribute, you need to retrieve this from element and assert as expected.

Sample HTML code:



This paragraph is made in bold using html ‘b’ tag. This paragraph is made in bold using html ‘strong’ tag.

This paragraph is made in bold using css attribute named font-weight.

Java Program:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class VerifyTextInBold {

        public static void main(String[] args) {
                
                // Launching browser and load html file
                System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                driver.get("file:///C:/Users/amodm/fontWeight.html");
                
                // Assert tag name of bold text
                String tagName= driver.findElement(By.id("bold1")).getTagName();
                Assert.assertEquals(tagName, "b");
                
                // Assert tag name of bold text
                String tagName1= driver.findElement(By.id("bold2")).getTagName();
                Assert.assertEquals(tagName1, "strong");
                
                // Get value of font-weight and assert if it is bold
                String fontWeight= driver.findElement(By.id("bold3")).getCssValue("font-weight");
                System.out.println(fontWeight);
                Assert.assertTrue(Integer.parseInt(fontWeight)>700);
                
                driver.quit();
                
                
        }
}

In above example, I consider bold text but you can change it as per your requirement. If you are confused with how to get css value- Read this post.

In case of any doubt, suggestion or you find some mistake, feel free to let me know in comments.

#ThanksForReading

Uncategorized

Post navigation

Previous Post: Postman Tutorial Part 27 – Building Workflows in Collection Runner Using setNextRequest() in Postman
Next Post: Collections in postman

Related Posts

Git Tutorial 26 – How To Ignore Already Indexed/staged/Committed/Pushed File to Git Repo Uncategorized
Interview Experience at CTS (Cognizant) Bangalore for Selenium Java Profile ( Sep – 2019) Uncategorized
Make Selenium Easy – Page 8 Uncategorized
Difference Between Constructor and Method in Java Uncategorized
TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities Uncategorized
Best Practises in Page Object Model : Naming Conventions of Web Elements & Actions on it | Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark