无法通过使用selenium和python将文件通过sendkeys上传到iframe中的按钮元素 [英] Unable to upload file by sendkeys into button element within iframe using selenium with python

查看:183
本文介绍了无法通过使用selenium和python将文件通过sendkeys上传到iframe中的按钮元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Environment]:Python + Selenium

我试图上传一个本地文件到上传文件按钮。

首先,我试着查看是否可以找到该元素并单击该按钮,然后使用

  driver.switch_to_frame(upload_frame)
driver.find_elements(By.ID,'id_file')。click()

所以,我用同样的方法,用<$ c替换 click()
$ b

  driver.switch_to_frame(upload_frame)$ c $> send_keys()用于文件上传。 b $ b driver.find_elements(By.ID,'id_file')。send_keys(xxx.bin)





因此,我尝试使用其他定位器,如下所示:(它们都不能工作)

  driver.find_element(By.XPATH,// button [text()='Update from File'])
driver。 find_elements(By.XPATH,// * [@ id ='id_file'])
driver.find_elements(By.XPATH,// input [@ id ='file'])

另外,我还用它搜索了很多类似的任务离子,但无法找到解决方案/答案。

想征询你的意见,并让我对此有所了解?
谢谢。


HTML codesnippet:

 < ; iframe id =upload_frameheight =30pxframeborder =0width =0src =/ web / setting / upload.html?r = 1422498136526scrolling =no
name = upload_framestyle =width:170px;>
<!DOCTYPE html>
< html>

< head>

< body onload =page_load();>
< div id =contentclass =b1>
< input type =hiddenvalue =cgi_firmware_uploadname =cmd>
< div class =file_input_div>


解决方案

driver.find_elements(By .ID,'id_file')。send_keys(xxx.bin)将不起作用,因为它对应于按钮元素而不是 input元素与类型文件。

对于使用硒进行简单文件上传,您必须首先搜索输入标记为文件类型。就像你的代码那样

< input id =fileclass =file_input_hiddentype =文件onchange =start_upload();的onclick = clear_upload_path(); style =cursor:pointername =file>



请使用下面的代码让文件上传工作: / p>

  driver.switch_to_frame(upload_frame)
driver.find_element(By.ID,'file')。send_keys '//上传文件的路径')

注意: - 以上对应于类型为文件的输入标签。


[Environment]: Python + Selenium

I am trying to upload a local file to the upload file button.

First of all, I tried to see if I can locate that element and click on that button and I succeed by using

driver.switch_to_frame("upload_frame")
driver.find_elements(By.ID, 'id_file').click()

So, I used the same way but replace the click() with send_keys() for file uploading.

driver.switch_to_frame("upload_frame")
driver.find_elements(By.ID, 'id_file').send_keys("xxx.bin")

But it can not pass the value in.

So, I tried to use other locator as following: (None of them can work)

driver.find_element(By.XPATH, "//button[text()='Update from File']")
driver.find_elements(By.XPATH, "//*[@id='id_file']")
driver.find_elements(By.XPATH, "//input[@id='file']")

In addition, I also googled it with so many similar questions but unable to find a solution/answer.

Would like to seek your advice and shed me some light on this? Thank you.

HTML codesnippet:

<iframe id="upload_frame" height="30px" frameborder="0" width="0" src="/web/setting/upload.html?r=1422498136526" scrolling="no"
name="upload_frame" style="width: 170px;">
    <!DOCTYPE html>
    <html>

    <head>

        <body onload="page_load();">
            <div id="content" class="b1">
                <form id="form_firm" action="/cgi-bin/system_mgr.cgi" enctype="multipart/form-data" method="post" name="form_firm">
                    <input type="hidden" value="cgi_firmware_upload" name="cmd">
                    <div class="file_input_div">
                        <button id="id_file" type="button" style="border: 2px solid rgb(70, 70, 70); background: none repeat scroll 0% 0% rgb(33, 33, 33);">
                            <span class="_text" lang="_firmware" datafld="update_b">Update from File</span>
                        </button>
                        <input id="file" class="file_input_hidden" type="file" onchange="start_upload();" onclick="clear_upload_path();" style="cursor:pointer"
                        name="file">
                    </div>
                </form>
            </div>
        </body>

    </html>
</iframe>

解决方案

driver.find_elements(By.ID, 'id_file').send_keys("xxx.bin") won't work as it corresponds to the button element not the input element with type file.

For simple file upload using selenium, you must first search for the input tag with type as file. As in your code, that must be:

<input id="file" class="file_input_hidden" type="file" onchange="start_upload();" onclick="clear_upload_path();" style="cursor:pointer" name="file">

Please use the below code for the file-upload to work:

driver.switch_to_frame("upload_frame")
driver.find_element(By.ID, 'file').send_keys('//path of the file to upload')

Note:- The above corresponds to the "input tag with type file".

这篇关于无法通过使用selenium和python将文件通过sendkeys上传到iframe中的按钮元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆