隐藏文件输入Python Selenium

2024-09-30 16:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图使用pythonseleniun将文件上传到隐藏的文件输入时,我遇到了一个问题。为了更清楚,请看下面的图片。在

Issue example

我自己为第一个字段上传了一个文件。 下面是一个相应的HTML代码在上传前后的示例。在

<judge-upload caption="Output to be scored" is-uploading="submissionsCtrl.isUploading[$index]" ng-model="submissionsCtrl.newSubmissions[$index].submissionBlobKey" class="ng-pristine ng-untouched ng-valid ng-scope ng-not-empty" aria-invalid="false" style=""><div class="hcj-upload"> <md-input-container class="md-input-has-value"> <div class="hcj-upload-label ng-binding">Output to be scored</div> <div class="hcj-upload-content" ng-switch="!ctrl.fileKey"> <!-- ngSwitchWhen: true --> <!-- ngSwitchWhen: false --><div ng-switch-when="false" layout="row" class="ng-scope layout-row" style=""> <div> <input value="example0.out" aria-label="Filename" readonly="" class="md-input" id="input_8"><div class="md-errors-spacer"></div> </div> <div> <button class="md-icon-button md-button md-ink-ripple" type="button" ng-transclude="" ng-click="ctrl.onClear()" aria-label="Clear"> <md-icon class="ng-scope material-icons" role="img" aria-hidden="true">delete</md-icon> </button> </div> </div><!-- end ngSwitchWhen: --> </div> </md-input-container> </div> </judge-upload> ************************************* <judge-upload caption="Output to be scored" is-uploading="submissionsCtrl.isUploading[$index]" ng-model="submissionsCtrl.newSubmissions[$index].submissionBlobKey" class="ng-pristine ng-untouched ng-valid ng-scope ng-empty" aria-invalid="false"><div class="hcj-upload"> <md-input-container class=""> <div class="hcj-upload-label ng-binding">Output to be scored</div> <div class="hcj-upload-content" ng-switch="!ctrl.fileKey"> <!-- ngSwitchWhen: true --><div ng-switch-when="true" layout="row" class="ng-scope layout-row"> <button class="md-raised md-primary md-button md-ink-ripple" type="button" ng-transclude="" ng-click="ctrl.onUploadClick($event)" ng-disabled="!ctrl.ready || ctrl.fileName" aria-label="Upload">Upload file</button> <input class="hcj-upload-input ng-pristine ng-untouched ng-valid md-input ng-empty" ng-model="ctrl.fileValue" aria-label="Input file" id="input_3" aria-invalid="false" type="file"><div class="md-errors-spacer"></div> <div class="hcj-upload-filedrag layout-align-center-center layout-row" layout="row" ng-show="!ctrl.fileName" layout-align="center center" drag-drop="" on-drag="ctrl.onDragOver($event, $enter)" on-drop="ctrl.onDrop($event)" aria-hidden="false"> or drop file here </div> <div class="hcj-upload-loading ng-hide layout-align-center-center layout-row" layout="row" ng-show="ctrl.fileName" layout-align="center center" aria-hidden="true"> Uploading file. Please wait. </div> </div><!-- end ngSwitchWhen: --> <!-- ngSwitchWhen: false --> </div> </md-input-container> </div> </judge-upload>

使用selenium,我尝试了多种方法来上载文件,但都不起作用:

^{pr2}$

在第二个文件之后,html似乎用文件路径进行了更新,但它不会在web浏览器中显示它。在

你能帮我一下吗?在

谢谢!在

------编辑------ 当我使用第二种方法发送文件时,我遇到的问题是submit按钮仍然被禁用。按钮的代码看起来像

<button class="md-primary md-button md-ink-ripple" type="button" ng-transclude="" ng-click="submissionsCtrl.createSubmission()" ng-disabled="!submissionsCtrl.canUploadSubmissions()" disabled="disabled">Submit</button>

我试图通过Python代码启用它,但它目前还没有起作用,如果你有任何想法的话,它仍在努力。在

driver.execute_script('document.getElementsByClassName("md-primary md-button md-ink-ripple")[0].disabled=false')
driver.find_elements_by_xpath("//*[@ng-click='submissionsCtrl.createSubmission()']")[0].click()
print driver.find_elements_by_xpath("//*[@ng-click='submissionsCtrl.createSubmission()']")[0].is_enabled()

即使在脚本调用之后,单击按钮也不执行任何操作,否则对于isu enabled,输出仍然显示False。在

编辑2------ 我终于成功地启用了按钮并点击它。不幸的是,看起来这些文件并不是真正由表单发送的。在

我还在调查为什么这些文件没有被发送。在

我用来启用按钮的代码:

driver.execute_script('''document.evaluate("//*[@ng-click='submissionsCtrl.createSubmission()']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.disabled=false''')
driver.find_elements_by_xpath("//*[@ng-click='submissionsCtrl.createSubmission()']")[0].click()

Tags: 文件divfalseinputbuttonngmdclass
1条回答
网友
1楼 · 发布于 2024-09-30 16:24:57

因为您的页面是AngularJs页面,所以文件输入被绑定到模型ng-model="ctrl.fileValue"。在

即使您使用execute_script以后台方式设置值,但这种方式不会触发模型fileValue更改。在

我想UPLOAD FILE按钮的click eventng-click="ctrl.onUploadClick($event)"将从模型fileValue获得文件路径,而不是从document.getElementById("input_3").value获取文件路径。在

所以即使没有错误报告,也不能成功上传文件。在

要解决您的问题,唯一的方法是直接更改模型fileValue的值:

def input_upload_file_path(file_control, file_path):
    script = '''
       angular.element(arguments[0]).scope().$apply('fileValue=arguments[1]')
    '''
    driver.execute_script(script, file_control, file_path)

file_control = driver.find_element_by_css_selector('#input_3')
file_path = <absolute path of file>

input_upload_file_path(file_control, file_path)

在执行input_upload_file_path()之后,在这里添加一个暂停,然后您可以通过在浏览器DevTool的Console选项卡中的Javascript下面执行来检查函数是否正常工作:

^{pr2}$

应打印与input_upload_file_path()参数值相同的文件路径

enter image description here

相关问题 更多 >