Fri Gate
Fri Gate

Reputation: 31

Image/file upload on Selenium

I'm kinda stuck on how to get past this hurdle. I'm trying to make a reposter for Kijiji, and I'm stuck on how to handle file upload when I'm on Mac. I tried to do

driver.find_element_by_id('ImageUploadButton').send_keys(image)

But that doesn't seem to do anything, I believe that might be because of kijiji's special file upload, but I'm not sure how to get past this hurdle.

Has anyone done this before?

The code on their "View Source" page:

<div id="ImageUpload" class="clearfix form-section placeholders">

    <p class="images-title">Add at least one photo. Use more to show different angles and details.</p>
        <ol id="UploadedImages">
        </ol>

    <span class="field-message" data-for="FileUploadInput"></span>

            <div id="ImageDragAndDrop" class="clearfix">
                <div class="image"></div>
                <div class="copy">
                    <h3>Drag and Drop</h3>
                    <p>Drag and drop to change the order of your pictures.</p>
                </div>
            </div>

            <div id="FileInputWrapper" class="file-input-wrapper">
                <input type="hidden" name="file" id="FileUploadInput" >

                <h3>Get at least twice the number of replies by uploading images</h3>
                <p>You can upload a maximum of <span id="MaxImages">10</span> photos, that are at least 300px wide or tall (we recommend at least 1000px).</p>

                <button id="ImageUploadButton" type="button" class="button-update-cancel short file-upload-button">
                    Select Images</button>
            </div>
        <input type="hidden" name="images">
    </div>

Upvotes: 1

Views: 629

Answers (2)

Jester Dev
Jester Dev

Reputation: 11

I coded mine in c# but the language format can be changed to work. For kijiji image posting I had to use the following:

js.ExecuteScript("arguments[0].setAttribute('style', arguments[1])", driver.FindElement(By.XPath("//input[@type='file']")), "0");
driver.FindElement(By.XPath("//input[@type='file']")).SendKeys("path of file and filename");

Note: repeat the last line for each image you want to upload.

Upvotes: 1

alecxe
alecxe

Reputation: 474251

You need to target the "file" input element instead of a button:

image_input = driver.find_element_by_id("FileUploadInput")

Now, the problem is, this element is hidden and sending keys would not work as is. To solve this problem, you need to make the element visible first:

driver.execute_script("arguments[0].type = 'file';", image_input)
image_input.send_keys(image)

Upvotes: 0

Related Questions