how to get a dynamic value from a website using selenium C#?

I have a problem, I want to write a code in selenium C# that getting dynamic value from a webpage and display in my APP, The value of the variable within the webpage changes frequently and I want to display this value in my APP,I use a textbox to display variable value, this is my code:

textBox2.Text=driverA.FindElement(By.Id("stock_ClosingPrice")).Text;

but its a static text, and it does not display changes to the value of the variable ("stock_ClosingPrice") I don't really know that I can use selenium for this or not!? can everyone help me? thanks a lot

1 answer

  • answered 2021-01-11 06:59 Dylan Lacey

    Calling the .Text method returns the value of the innerText property of an element.

    For example, for this HTML:

    <p id="demo">   This element has extra spacing     and contains <span>a span element</span>.</p>
    

    The innerText is This element has extra spacing and contains a span element.. This is not the HTML of the element though; it's literally just the text. Notice the extra spaces have been removed, as has the span.

    (See the W3C Documentation for more; That's where I got that example from.)

    If you're after the value entered into a text box, you need to make two changes. Firstly, the value of a text box isn't part of the HTML, which is why calling .Text wasn't working. You need to use the getAttribute method to get the Value of the, uh, Value attribute:

    element.getAttribute("value");
    

    Secondly, I don't think you're selecting the right element. Your XPath is finding the first div, not the first input element. If you're after a text box, one a user could alter, you're not going to find it with that selector. I'd suggest either using the element ID, or using an input field selector, eg input[@type='text']