简体   繁体   中英

How to extract text from a web element using Selenium and C#?

I want to fetch the title (9,804,541) from this piece of Html Code.

<p class="ring_SecondaryValue" style="color: rgb(119, 119, 119);
 font-size: 16px; margin: 0px; overflow: hidden; text-align: center;
 text-overflow: ellipsis; vertical-align: middle; white-space:
 nowrap;"><div class="ring_secondaryMeasureSum"
 title="9,804,541">9,805K</div></p>

I had tried this piece of code:

string left_value =driver.FindElement(By.ClassName("ring_secondaryMeasureSum")).GetAttribute("title");

You can use like this

In java it is like this

  WebElement title = driver.findElement(By.xpath("//div[contains(@class,'ring_secondaryMeasureSum')][contains(text(),'')]"));
            System.out.println(title.getAttribute("title"));

I tried in c Sharp also, you may check syntax error as I am not good in c sharp

IWebElement title = driver.FindElement((By.Xpath("//div[contains(@class,'ring_secondaryMeasureSum')][contains(text(),'')]"));

string titleText= title.GetAttribute("title");

As per the HTML you have shared to extract the title 9,804,541 you can use either of the following solution:

  • CssSelector :

     string left_value = driver.FindElement(By.CssSelector("p.ring_SecondaryValue > div.ring_secondaryMeasureSum")).GetAttribute("title"); 
  • XPath :

     string left_value = driver.FindElement(By.XPath("//p[@class='ring_SecondaryValue']/div[@class='ring_secondaryMeasureSum']")).GetAttribute("title"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM