[环境]:Python + Selenium
我正在尝试将本地文件上传到上传文件按钮。
首先,我尝试查看是否可以找到该元素并单击该按钮,我通过使用成功了
driver.switch_to_frame("upload_frame")
driver.find_elements(By.ID, 'id_file').click()
所以,我用同样的方式,但替换click()
with send_keys()
用于文件上传。
driver.switch_to_frame("upload_frame")
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 代码片段:
<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>