Search code examples
c#watin

Get text from text field of type email?


So I am trying to read a site with C#. I have it so it opens a website and goes to read a TextField. However the website has the text field like so.

<input name="loginMail" class="c-txtfld c-txtfld-fixed c-txtfld-green c-txtfld-medium" id="login-email" type="email" placeholder="Email address" pattern="[^ @]*@[^ @]*">

And because of this I can get C# to read it.

TextField emailaddressfield = this.Document.TextField(Find.BySelector("form#login-form.credential-form input#login-email")

It will work however if I edit the type attribute to text.

I want to keep this as email. How can I read it like that?


Solution

  • It is possible to create a custom control from existing WatiN controls (HTML). In order to get text field value of type email, we need to create a custom control which would be of type TextField. Please try below code.

    [ElementTag("input", InputType = "email", Index = 5)]    
    public class EmailTextField : Element<EmailTextField>
    {
        public EmailTextField(DomContainer domContainer, ElementFinder finder): base(domContainer, finder)
        { }
        public EmailTextField(DomContainer domContainer, INativeElement element): base(domContainer, element)
        { }
    }
    
    EmailTextField emailaddressfield = this.Document.EmailTextField(Find.BySelector("form#login-form.credential-form input#login-email")
    

    (Pseudo code - not complied)