Getting attribute value using Selenium

Issue

This is my html page i want to parse:

<html lang="en-US" data-ismobile="false">
    <body ond="return false;" onselect="return false;" textmenu="return false">
        <div id="wrap">
            <form id="frm1" method="post">
                <input name="Token" type="hidden" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX">
                <input type="hidden" name="cate" value="0">
                <input type="hidden" name="sub" value="7">
            </form>
            <form id="frm2" method="post">
                <input name="Token" type="hidden" value="YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY">
                <input type="hidden" name="cate" value="0">
                <input type="hidden" name="sub" value="7">
            </form>
        </div>
    </body>
</html>

I am trying to get name & value attributes from form frm1

So far i have come up with following code:

IWebElement _frm1 = webDriver.FindElement(By.Id("frm1")); //this should find me a correct form
token_frm1 = _frm1 .GetAttribute("value"); //this i know is inccorect 

Solution

form node has neither @name nor @value. Try to get attribute values from child inputnode

IWebElement _frm1 = webDriver.FindElement(By.Id("frm1"));
IWebElement _input = _frm1.FindElement(By.Name("Token"));
token_frm1 = _input .GetAttribute("value");

Answered By – JaSON

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published