简体   繁体   English

使用Selenium Webdriver自动上传文件

[英]Automating File Upload with Selenium Webdriver

Currently I am trying to automate a HTML file upload. 目前,我正在尝试自动执行HTML文件上传。

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的代码

<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. 现在,我希望发送原始javascript来click()该对象,或者甚至选择具有By.Id('')的元素并以这种方式打开它,但这似乎不起作用。

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. 发送的所有密钥都必须定向到Selenium WebDriver,而不是从Windows本身执行,因为用户将与WinForm进行交互。

You can use the .SendKeys() method to do this. 您可以使用.SendKeys()方法执行此操作。 eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM