【问题标题】:Image/file upload on SeleniumSelenium 上的图像/文件上传
【发布时间】:2016-11-10 14:22:12
【问题描述】:

我有点不知道如何克服这个障碍。我正在尝试为 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>

【问题讨论】:

  • 为文件输入元素提供HTML代码
  • 这是它的截图。 imgur.com/a/BRueF
  • 我很确定您不能将文本发送到button 类型的元素。检查你的页面元素&lt;input type=file"&gt;

标签: python python-2.7 selenium


【解决方案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");

注意:为您要上传的每张图片重复最后一行。

【讨论】:

    【解决方案2】:

    您需要定位“文件”input 元素而不是按钮:

    image_input = driver.find_element_by_id("FileUploadInput")
    

    现在,问题是,这个元素是隐藏的,发送密钥无法正常工作。要解决这个问题,你需要先让元素可见

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-19
      • 2022-11-25
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2014-07-14
      相关资源
      最近更新 更多