1

Please find below the sample HTML code:

<div class="form-element email-address" id="gmail-address-form-element">
  <label id="gmail-address-label">
  <strong>
  Choose your username
  </strong>
  <input type="text" maxlength="30" autocomplete="off"  
  name="GmailAddress" id="GmailAddress" value=""
spellcheck="false">
  <span class="atgmail">@gmail.com</span>
  </label>

I need to verify "Choose your username" label is bold with Selenium WebDriver. Appreciate if the language used is java.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Tijo Thomas
  • 23
  • 1
  • 7
  • possible duplicate of http://stackoverflow.com/questions/38497379/how-to-verify-that-a-word-is-bold-in-selenium-webdriver-using-java – Paras Jul 21 '16 at 12:25
  • You could just check for the `STRONG` tag and make sure the text is inside. – JeffC Jul 21 '16 at 13:49
  • Possible duplicate of [How to verify bold appearance of a certain field in selenium](https://stackoverflow.com/questions/10100438/how-to-verify-bold-appearance-of-a-certain-field-in-selenium) – Nakilon Jul 09 '19 at 11:09

2 Answers2

0

You can use the getComputedStyle to get the style of the element.

Use font-Weight to check if it is bold or not

    JavascriptExecutor js = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(By.cssSelector("#gmail-address-label > strong"));
    String fontWeight = (String) js.
            executeScript(
                    "return getComputedStyle(arguments[0]).getPropertyValue('font-Weight');",
                    element);
    if (fontWeight.trim().equals("bold")) {
        System.out.println("Is Bold");
    } else {
        System.out.println("Not Bold - " + fontWeight);
    }
Community
  • 1
  • 1
Madhan
  • 5,750
  • 4
  • 28
  • 61
0

In simple way we can get the CssValue of font-weight and check if it is 'bold' or is greater than or equal to 700 or is 'bolder' .Because by all these ways the bold style can be given to the text Use following code in java

 String fontWeight = driver.findElement(By.className("classname"))
                                          .getCssValue("font-weight");

 boolean isBold = "bold".equals(fontWeight) || "bolder".equals(fontWeight) || Integer.parseInt(fontWeight) >= 700;

Hope it will help.

CVitthal
  • 11
  • 3