Danielle
Danielle

Reputation: 97

C# web browser automation

I am a newbie in web browser automation, and I choose Google pages as the samples to learn.

I try to simulate a file upload to Google Drive, but I encounter several problems.

http://s4.postimage.org/jbx6c3q3h/Untitled_1.jpg

private void webBrowser_DocumentCompleted ( object sender, WebBrowserDocumentCompletedEventArgs e ) {
    HtmlElement element = webBrowser.Document.GetElementById( "contentcreationpane" );
    if ( element != null )
        UploadFile();
}


private void UploadFile () {
    HtmlElementCollection elements = webBrowser.Document.GetElementsByTagName( "div" );
    foreach ( HtmlElement element in elements ) {
        if ( element.GetAttribute( "data-tooltip" ) == "Upload" ) {
            element.InvokeMember( "click" );
            break;
        }
    }

    HtmlElement uploadButton = webBrowser.Document.GetElementById( ":1" );
    if ( uploadButton != null ) {
        uploadButton.InvokeMember( "click" );
    } else {
        Exception goes here! Is it necessary to do a wait after previous element "Upload" is clicked?
    }
}

Upvotes: 2

Views: 1495

Answers (3)

Volokh
Volokh

Reputation: 390

You can use a mixture of Selenium and Sikuli. Selenium is really a great tool for browser automation. However in case you have to use UI elements which are not automatable by Selenium, I would recommend to use Sikuli for C#. Here I have found an example how it could look like:

    Launch.Start();  
    IwebDriver driver = new ChromeDriver("D:/>SeleniumDrivers/ChromeDriver32");  
    driver.Manage().Window.Maximize();  
    driver.Navigate.GoToURL("http://www.csharpcorner.com/");  
    Screen scr = new Screen();  
    scr.Click(Image1, true);  
    scr.wait(10);  
    scr.Type(Image2, "string to Enter", KeyModifier.None);  
    scr.wait(10);  
    scr.Type(Image3, "string to enter", keyModifier.None);  
    scr.wait(10);  
    scr.Type(Image4, "string to enter", KeyModifier.None);  
    scr.wait(10);  
    scr.Type(Image5, "string to enter", KeyModifier.None);  
    scr.wait(10);  
    driver.quit();  

You can easily interact with UI elements and simulate keyboard input with Sikuli.

Upvotes: 1

Bill Agee
Bill Agee

Reputation: 3646

This may be intentionally hard to automate - I know GMail (and Yahoo and other webmail providers) purposefully make it complicated to automate sending email via their web interfaces.

So uploading documents to Google Drive may be similar; it's likely that to prevent abuse, Google has put roadblocks in place to make it difficult to automate uploads.

Upvotes: 1

alex
alex

Reputation: 12654

You can try to use some kind of web UI testing framework, like Selenium. It can help you record the test case and can even generate C# code to paste in your test project.

Upvotes: 1

Related Questions