我有点坚持如何克服这个障碍。我正在尝试为Kijiji制作一个转发器,当我在Mac上时,我仍然坚持如何处理文件上传。我试着做
driver.find_element_by_id('ImageUploadButton').send_keys(image)
但这似乎没有做任何事情,我相信这可能是因为kijiji的特殊文件上传,但我不知道如何克服这个障碍。
以前有人这样做过吗?
“查看来源”页面上的代码:
<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>
答案 0 :(得分:1)
我用C#编写了我的代码,但是语言格式可以更改为可用。对于kijiji图片发布,我必须使用以下内容:
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");
注意:对于您要上传的每个图像重复最后一行。
答案 1 :(得分:0)
您需要定位“文件”input
元素而不是按钮:
image_input = driver.find_element_by_id("FileUploadInput")
现在问题是,这个元素是隐藏的,发送密钥不会按原样运行。要解决此问题,您需要首先使元素可见:
driver.execute_script("arguments[0].type = 'file';", image_input)
image_input.send_keys(image)