Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
824 views
in Technique[技术] by (71.8m points)

css - How can I verify text is bold using selenium on an angular website with C#

I am trying to verify whether text is bold or not, within a free text area. When I select the element, I cannot verify the text part.

I have tried using .getCSSValues as per the duplicate link suggestion but it doesn't work as it doesn't get the 'text' of that freetext area, which is a string. The freetext area is an element.

IWebElement isBold = _driver.FindElement(By.TagName("p"));
isBold.GetCssValue("font-weight");

But the font weight returns "400" regardless of whether the text is bold or not.

The HTML is

<div class="fr-element fr-view" dir="auto" contenteditable="true" aria-disabled="false" spellcheck="true"><p style=""><strong>TEXT</strong></p></div>

I would expect the selected text to be "700" when it is bold.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems you were close. You need to consider the following facts:

  • As the element is an Angular element so to locate the element you have to induce WebDriverWait for the ElementIsVisible()
  • Next you can use the GetCssValue() method to extract the font-weight
  • You can use either of the following Locator Strategies:

    • cssSelector:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div.fr-element.fr-view>p>strong"))).GetCssValue("font-weight");
      
    • xpath:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@class='fr-element fr-view']/p/strong[text()='TEXT']"))).GetCssValue("font-weight");
      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...