3

I want to upload 5 files,but my 'file input' is same name/id,how can i possible to upload five files. My HTML code is:

<div>
    <table id="listtable">
    </table>
    <br/>
    <input type="hidden" name="delFiles" id="deletefiles"/> 
    <table id="filetable">
    <tbody>
        <tr>
            <td>
                <input type="file" size="27px" id="page" name="page"/>
            </td>
            <td>
                <a href="#">
                    <img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
                </a>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" size="27px" name="page"/>
            </td>
            <td>
                <img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" size="27px" name="page"/>
            </td>
            <td>
                <img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" size="27px" name="page"/>
            </td>
            <td>
                <img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" size="27px" name="page"/>
            </td>
            <td>
                <img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
            </td>
        </tr>
        </tbody>
    </table>
    <br/>
    <br/>
</div>
Arran
  • 24,648
  • 6
  • 68
  • 78
Namitha
  • 255
  • 3
  • 9
  • 16
  • 2
    You really need to review all your asked questions and accept answers. If no answers are satisfied, please provide feedbacks to the repliers. http://meta.stackexchange.com/a/5235 – Yi Zeng Jun 20 '13 at 10:24
  • @Namitha, your HTML will not shown unless you indent it by a tab or four spaces. Do not rollback the edit I made, or simply do this yourself. – Arran Jun 20 '13 at 10:34
  • Possible duplicate of [webdriver:upload multiple files](https://stackoverflow.com/questions/23955430/webdriverupload-multiple-files) – Alex Kulinkovich Aug 06 '19 at 12:15

4 Answers4

15

This works on Chrome:

driver.findElement(By.id("input1")).sendKeys("path/to/first/file-001 \n path/to/first/file-002 \n path/to/first/file-003");
Leopold Joy
  • 4,524
  • 4
  • 28
  • 37
Martin Zamora
  • 169
  • 1
  • 4
  • 1
    works for me as well :) For everyone else who is encountering errors, just make sure that you're sending everything as a single string and not calling `sendKeys` more than once. – Clement Jul 10 '18 at 02:54
  • not work for me, what's your chrome version? – CodeAlien May 20 '21 at 07:11
1

You would do so the same as you would if you were only uploading one file.

driver.findElement(By.id("input1")).sendKeys("path/to/first/file");
driver.findElement(By.id("input2")).sendKeys("path/to/second/file");
driver.findElement(By.id("input3")).sendKeys("path/to/third/file");
driver.findElement(By.id("input4")).sendKeys("path/to/fourth/file");
driver.findElement(By.id("input5")).sendKeys("path/to/fifth/file");
driver.findElement(By.id("upload")).click();

Obviously, you'll need to put in your own correct IDs or whatever.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • But here all textboxs name=page.So how can I give like this? – Namitha Jun 20 '13 at 10:17
  • 3
    In that case I guess you'd need to make a list of the `input` elements first. `inputs = driver.findElements(By.xpath("//input[@type='file']");` `inputs[1].sendKeys("path/to/first/file");` `inputs[2].sendKeys("path/to/second/file");` `inputs[3].sendKeys("path/to/third/file");` `inputs[4].sendKeys("path/to/fourth/file");` `inputs[5].sendKeys("path/to/fifth/file");` But it would be far better if you could iterate over the list whilst adding in a string that contains the paths from an array using the same index position. – Mark Rowlands Jun 20 '13 at 11:05
0

that easy like //input[@type="file"] will point to the first input tag and (//input[@type="file"])[{INDEX}] where INDEX is the number of the of the input tag note: indexing in xpath starts from 1

OR you can use the

file_tag_list =driver.find_elements_by_xpath(//input[@type="file"])

function that the python syntax you can find it for different languages just google it. this function will return a list of webdriver element and then you can

file_tag_list[0].send_keys(filepath)
file_tag_list[1].send_keys(filepath)
Bilal Naqvi
  • 140
  • 1
  • 11
-1

Holy ##### it even works in PHP:

public function waitForAjax()
{
    while(true)
    {
        $ajaxIsComplete = array(
            'script' => 'return jQuery.active == 0',
            'args' => array()
        );
        $ajaxIsComplete = $this->execute($ajaxIsComplete);
        if ($ajaxIsComplete) {
            break;
        }
    }
}

Thank you :)

Simon Brown
  • 127
  • 1
  • 6