Matt Biggs
Matt Biggs

Reputation: 177

Automating File Upload with Selenium Webdriver

Currently I am trying to automate a HTML file upload.

For what ever reason the website that I am trying to do this on does not make it as straight forward as I would like them to make it..

This is their code for the file upload div

<div class="form-row">
    <div id="fileupload" class="fileupload">

    <div class="c-position-relative margin-vertical10">
            <ul id="loaded-files" class="upload-image-thumbs clearfix loaded-placeholder">
                <li class="upload-placeholder upload-image">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder new-line">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        <li class="upload-placeholder ">
                            <div class="uploadedImg"></div>
                        </li>
                        </ul>
            <div id="upload_btn" class="c-green-button c-rounded-corners5 c-large">
                Add pictures
                <input type="file" name="file" multiple="">
            </div>

Now I would love to either send raw javascript to click() the object or even select the element with By.Id('') and open it this way but this does not seem to work.

I know the element can be opened when it is highlighted and an enter key is sent by yet again I cannot seem to get this to work.

Looking for some ideas and/or solutions.

All keys that are sent need to be directed to the Selenium WebDriver rather than being executed from windows itself as the user will be interacting with a WinForm.

Upvotes: 1

Views: 2649

Answers (1)

Jamie Rees
Jamie Rees

Reputation: 8183

You can use the .SendKeys() method to do this. e.g.

var uploadBtn = WebDriver.FindElement(By.Id("upload_btn"));
uploadBtn.SendKeys("C:\\FilePath\\File.txt");

Upvotes: 4

Related Questions