以水平形式输入文本

时间:2019-02-17 01:59:52

标签: python-3.x selenium

我有一个带有上传按钮的水平表单,就像下面的源代码一样。我正在尝试使用硒将下面的“ newRes”文件路径输入到水平框中。我已经尝试了下面所有的代码行。我已对每次失败的尝试进行了评论。它们都引发与找不到元素有关的错误。我是硒的新手,请让我知道我应该在下面寻找源代码的哪一部分,以及有关如何在当前显示“ oldFile.docx”的框中输入“ newRes”文件路径的任何提示,将不胜感激。

代码:

newRes='/Users/new_file.docx'

# upload_element = driver.find_element_by_link_text(“oldFile.docx")
# upload_element = driver.find_element_by_link_text("Upload")
# upload_element = driver.find_element_by_id('resume')
# upload_element = driver.find_element_by_id('resumeUpload')
upload_element.send_keys(newRes)#+keys.Keys.ENTER)


# assert 'Resume' in driver.page_source
# action = action_chains.ActionChains(driver)

# action.send_keys(newRes)
# action.perform()

源代码:

<div class="form-group has-feedback" data-ng-switch="isOnEditMode" data-ng-class="{'has-success': (profile.resume.fileName &amp;&amp; frmInputResume.resumeFile.$error.diceFileFormat==false &amp;&amp; frmInputResume.resumeFile.$error.diceMaxFileUploadSize==false), 'has-error': (!profile.resume.fileName || frmInputResume.resumeFile.$error.diceFileFormat == true || frmInputResume.resumeFile.$error.diceMaxFileUploadSize == true)}">
            <!-- ngSwitchWhen: true --><div data-ng-switch-when="true" class="ng-scope">
                <label class="col-md-3 profile-summary-label">Résumé:</label>
                <div class="col-md-6 col-lg-7 col-xs-12">
                    <div id="resumeUpload" class="file-input-btn btn-block">
                        <input type="file" name="resumeFile" id="resumeFileInput" data-dice-file-input-reader="" data-file-model="resume" data-ng-model="resumeInput" data-supported-file-extension-pattern="diceRegex.supportedFileExtensions" data-max-file-upload-size="2048000" data-file-read-callback="uploadResume(resume)" class="ng-pristine ng-valid">
                        <span class="btn btn-default btn-lg btn-file-name ng-binding" data-ng-class="{'text-muted': !profile.resume.fileName}">old_file.docx</span>
                        <span class="btn btn-default btn-lg btn-file-choose">Upload</span>
                    </div>
                    <span class="glyphicon form-control-feedback" style="line-height:46px; height:46px; width:46px;" data-ng-class="{'icon-filled-check-4': (profile.resume.fileName &amp;&amp; frmInputResume.resumeFile.$error.diceFileFormat == false &amp;&amp; frmInputResume.resumeFile.$error.diceMaxFileUploadSize==false), 'icon-filled-delete-2': (!profile.resume.fileName || frmInputResume.resumeFile.$error.diceFileFormat==true || frmInputResume.resumeFile.$error.diceMaxFileUploadSize == true)}"></span>
                    <span class="error ng-hide" ng-show="frmInputResume.resumeFile.$error.diceFileFormat" style="color: #C00;">We support .doc, .docx, .pdf, .txt, .rtf, up to 2MB.</span

1 个答案:

答案 0 :(得分:3)

请尝试以下代码。这将上传您想要的文件。

file_upload = driver.find_element_by_id("resumeFileInput")
file_upload.send_keys(/path/to/file) # Like "C:\user\desktop\test1.txt"

请让我知道是否适合您。