Search code examples
delphiiwebbrowser2

how do i login to gmail via internet explorer using iwebbrowser 2


is it possible to login to sites like facebook,gmail using iwebbrowser2?

in the code below what can i add so that it can login to gmail(with ie8 as browser) using the username and password i put in a variable?

MyBrowser := CreateOleObject('InternetExplorer.Application') as IWebBrowser2; 
MyBrowser.Navigate('http://mysite.com'..........???);

note : i am a newbie.

sorry for my english:)

and thanks in advance


Solution

  • You can use javascript 'injection' to control your site. By 'injection' I mean that once your gmail page is loaded you then construct a url in the form

    javascript:var Email = document.getElementById('Email');Email.value='User.Name';
    
    javascript:var Password = document.getElementById('Passwd');Password.value='YourPassword';
    
    javascript:var SignIn = document.getElementById('signIn');SignIn.click();
    

    or you can throw it all onto one URL

    javascript:var Email = document.getElementById('Email');Email.value='User.Name';var Password = document.getElementById('Passwd');Password.value='YourPassword';var SignIn = document.getElementById('signIn');SignIn.click();
    

    Your code would then look something like:

    MyBrowser := CreateOleObject('InternetExplorer.Application') as IWebBrowser2; 
    MyBrowser.Navigate('http://www.gmail.com');
    
    myUserName := 'User.Name';
    myPassword := 'password';
    loginURL:='javascript:var Email = document.getElementById(''Email'');Email.value=''' + myUserName + ''';var Password = document.getElementById(''Passwd'');Password.value=''' + myPassword + ''';var SignIn = document.getElementById(''signIn'');SignIn.click();';
    
    MyBrowser.Navigate(loginURL);