Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can I autofill input fields by code? #828

Closed
pipiscrew opened this issue Feb 20, 2015 · 36 comments
Closed

can I autofill input fields by code? #828

pipiscrew opened this issue Feb 20, 2015 · 36 comments

Comments

@pipiscrew
Copy link

in IE

            var theBrowser = (WebBrowser)wb;
            HtmlElement inputElementByName = GetInputElementByName("j_username", theBrowser);
            if (inputElementByName == null) return;
            inputElementByName.SetAttribute("value", "x@x.x");

in CefSharp (?)

thanks

@jornh
Copy link
Contributor

jornh commented Feb 20, 2015

Who ya' gonna call? http://youtu.be/m9We2XsVZfc 👻

No no no ... Call JS: https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#CallJS

@pipiscrew
Copy link
Author

wow! lets see!!

@amaitland
Copy link
Member

@pipiscrew Happy to close this one?

@pipiscrew
Copy link
Author

sorry for the delay, I done something like

        //the browser
        private readonly WebView web_view;

        public Form1()
        {
            InitializeComponent();

            web_view = new WebView("http://y.com/", new BrowserSettings());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            web_view.ExecuteScript("document.getElementById('email').value=" + '\'' + "takis" + '\'');
            web_view.ExecuteScript("alert('test');");
        }

worked!

thanks for the quick responses.

@jornh
Copy link
Contributor

jornh commented Feb 24, 2015

Excellent - thanks for sharing 👍 (and following up with closing) Marking as ``faq-able!`

@amaitland
Copy link
Member

@pipiscrew Out of curiosity are you still using CefSharp1?

@pipiscrew
Copy link
Author

@amaitland , I downloaded the CefSharp-v1.25.8-binaries from releases and using the lib, what are you mean by CefSharp1 ? the dlls built on 12/2/2015..

user_agent = Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22

wow, https://github.com/cefsharp/CefSharp/releases

the latest is
DEC 22, 2014, Chromium 39.0 / CEF 3.2171.1949

I downloaded the first one, seems incorrect.. is not the latest :(

please advise + whats new(?)

@amaitland
Copy link
Member

CefSharp1 is an old legacy version. v1.25.8 is based on Chromium 25. It's no longer officially supported (upstream work on it has ceased). One of the project maintainers released that particular version for his own personal requirements.

The official release is 37.0.0 which is based on Chromium 37. The next release (which will be in the next few weeks will be 39.0.0 which is based on you guessed it, Chromium 39.

We don't release binary versions in zip form anymore, they're all released as nuget packages.

@amaitland
Copy link
Member

I'd personally be using at a minimum version 37

@pipiscrew
Copy link
Author

what new (?) beside chromium updates, please.

thanks for warning

@amaitland
Copy link
Member

Too many things to list individually. Feel free to look at the changelog.

https://github.com/cefsharp/CefSharp/wiki/ChangeLog

@amaitland
Copy link
Member

(The fact that we no longer support CefSharp1 is a pretty big thing IMHO)

@pipiscrew
Copy link
Author

oh my god :)

I have to do a remake of the app :)

thanks for update me...!

check the freewares at http://bit.ly/1FT21o9

@amaitland
Copy link
Member

I have to do a remake of the app :)

In my experience it usually takes about 5-15mins to upgrade, a few things have changed, like WebView = ChromiumWebBrowser, that's the big one that gets most people.

check the freewares at http://bit.ly/1FT21o9

Is that a link to your app?

@pipiscrew
Copy link
Author

with v39pre2 dlls, I replace WebView with ChromiumWebBrowser as you mention above

        public Form1()
        {
            InitializeComponent();

            web_view = new ChromiumWebBrowser("http://x.com/api/");
            web_view.LoadCompleted += new LoadCompletedEventHandler(web_view_LoadCompleted);
            web_view.Dock = DockStyle.Fill;
            panel1.Controls.Add(web_view);
        }

        //url watcher for autofill
        void web_view_LoadCompleted(object sender, LoadCompletedEventArgs url)
        {
            if (url.Url.ToLower().StartsWith("https://www.x.com/login.php?skip_api_login"))
            {
                web_view.ExecuteScriptAsync("document.getElementById('email').value=" + '\'' + General.login_name + '\'');
                web_view.ExecuteScriptAsync("document.getElementById('pass').value=" + '\'' + General.login_password + '\'');
                web_view.ExecuteScriptAsync("document.getElementById('login_form').submit();");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CEF.VisitAllCookies(d);
        }

*referenced dlls : *
CefSharp.dll
CefSharp.WinForms.dll

errors :
alt text

@pipiscrew pipiscrew reopened this Feb 25, 2015
@jankurianski
Copy link
Member

Use Cef instead of CEF and FrameLoadEnd instead of LoadCompleted: https://github.com/cefsharp/CefSharp/blob/master/CefSharp.WinForms/ChromiumWebBrowser.cs#L204

@pipiscrew
Copy link
Author

thanks for the quick response

alt text

[edit]
CefSharp.Core.dll needed..

also the new version it doesnt appear on form! I have to use it on tabs (aka BrowserTabUserControl) ?

@jankurianski
Copy link
Member

If the browser doesn't appear on the form when you run it, try going through these troubleshooting steps (particularly point 4): https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting

You should see a process called CefSharp.BrowserSubprocess.exe running in Task Manager.

@pipiscrew
Copy link
Author

Im on x64 machine, using x86 libs as
alt text

then get files (as you suggested^) from NuGet
alt text

the same I got a blank form!

[edit]

Cef.Initialize(new CefSettings()); --> REQUIRED!

        public Form1()
        {
            InitializeComponent();

            Cef.Initialize(new CefSettings());

            web_view = new ChromiumWebBrowser("http://google.com/");

@pipiscrew
Copy link
Author

So, finally all functions working as constructed for v1.25.8...

ps - CefSharp3 is a monster!!

thanks

@jankurianski
Copy link
Member

@pipiscrew What did the problem turn out to be in the end? Anything you can share with future CefSharp 1 -> 3 upgraders like yourself?

@pipiscrew
Copy link
Author

pipiscrew commented Feb 26, 2015

as described above^

except turn type :

WebView to ChromiumWebBrowser

before instantiate ChromiumWebBrowser must initialize Cef

*CefSharp snippets : *
http://www.pipiscrew.com/2015/02/csharp-cefsharp-hints/

thanks!

@vip3r011
Copy link

vip3r011 commented Feb 22, 2018

what if your login form is like this below, what do i use then?

<input class="form-control" placeholder="Username" name="name" type="text">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
<input class="btn btn-lg btn-primary btn-block" id="submit" type="submit" value="Login">

@pipiscrew
Copy link
Author

last time I used was at 2015, dont know for newer version, as written above use Javascript code and execute it with Webview as (in the following example set the word takis to element has name="email") :

web_view.ExecuteScript("document.getElementById('email').value=" + ''' + "takis" + ''');

see the 2nd reply for details

@vip3r011
Copy link

vip3r011 commented Feb 23, 2018

the answer for my previous question:

void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    if (e.Url.ToLower().StartsWith("https://sitehere/login.html"))
    {
        browser.ExecuteScriptAsync("$('input[name=name]').val(\"user\")");
        browser.ExecuteScriptAsync("$('input[name=password]').val(\"password\")");
    }
}

@GlennIM
Copy link

GlennIM commented May 3, 2019

This code worked for me too as shown above...

    browser.ExecuteScriptAsync("$('input[name=name]').val(\"user\")");
    browser.ExecuteScriptAsync("$('input[name=password]').val(\"password\")");

But how did youI execute the submit button code below?

@pipiscrew
Copy link
Author

@GlennIM

if you form has id = login_form

web_view.ExecuteScriptAsync("document.getElementById('login_form').submit();");

more samples @ https://bit.ly/2vBOEZm

@chipotlejess
Copy link

I tried using the following, it didn't throw any errors but when I load the page, it doesn't plug the values into any of the fields...

private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { if (e.Url.ToLower().StartsWith("https://website.com")) { browser.ExecuteScriptAsync("$('input[name=Username]').val(\"user\")"); browser.ExecuteScriptAsync("$('input[name=Password]').val(\"user\")"); } }

I make sure to include
Cef.Initialize(new CefSettings());

P.S. I'm pretty new and still learning...

@totti240282
Copy link

but with CEF is possible use also Jquery for select elements ?

@pipiscrew
Copy link
Author

pipiscrew commented Jan 16, 2020

@chipotlejess, as @totti240282 says. Use vanilla JS :

document.getElementsByClassName('APassAttr')[0].value = 1 //element selector by CLASS (returns an array, then grab the first)

document.getElementsByName('activityTime')[0].value = '00'; //element selector by NAME (returns an array, then grab the first)

document.getElementById('CountryLocationAddress').value = 4020; 

@ghost
Copy link

ghost commented May 30, 2020

But how can i input the value onto the ID using CefSharp on VB .NET

@pipiscrew
Copy link
Author

pipiscrew commented May 30, 2020

see this

@ghost
Copy link

ghost commented May 30, 2020

okay that looks pro level, so i found this code snippet:

Dim EmailScript As String = <js><![CDATA[document.all("variable1").value = 'variable2';]]></js>.Value

        browser.ExecuteScriptAsync(EmailScript)

I can't get this to work because of the variables. How do I add VB.NET variables to a javascript code (the one above). Variable1 is string value which the user inputs in a TextBox and variable 2 is a constant string value.

@Ligh-one
Copy link

okay that looks pro level, so i found this code snippet:

Dim EmailScript As String = <js><![CDATA[document.all("variable1").value = 'variable2';]]></js>.Value

        browser.ExecuteScriptAsync(EmailScript)

I can't get this to work because of the variables. How do I add VB.NET variables to a javascript code (the one above). Variable1 is string value which the user inputs in a TextBox and variable 2 is a constant string value.

Got this working out? Just curious.

@darkanqeltr
Copy link

    private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { if (e.Url.ToLower().StartsWith("https://website.com")) { chrome.ExecuteScriptAsync("$('input[name=Username]').val(\"user\")"); chrome.ExecuteScriptAsync("$('input[name=Password]').val(\"user\")"); } } How can I use this code? I have to fill the form automatically and login. Thank you

@amaitland
Copy link
Member

Please ask questions on https://github.com/cefsharp/CefSharp/discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants