python - 使用 Selenium 和 ng-file-upload 自动上传文件

标签 python angularjs selenium automation ng-file-upload

我有一个项目,我通过 ng-file-upload 上传照片我需要使用 Python 中的 selenium webdriver 自动执行上传过程。

我的 HTML 元素如下所示:

<element
    ngf-pattern="image/*" accept="image/*"
    ngf-max-size="10MB" ngf-max-height="1000" ngf-select="addPhoto($index, $file)">
    ...</element>

手动上传元素绝对有效。但是我找不到在 Python 中使用 Selenium 来自动执行此操作的方法。我尝试找到元素,然后发送图像绝对路径的键,但这只是将绝对路径放在浏览器的搜索字段中(就像我在 Mac 上键入“Command + F”一样)

注意没有

<input type="file"/> 

使用这种上传文件的方法。

关于如何使用 Selenium 在 Python 中执行此操作的任何想法?谢谢!

最佳答案

必须有一个隐藏的文件输入,它隐含地负责文件上传。例如,angular-file-upload DEMO page是否隐藏在页面底部。

这是一个工作示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.get("https://angular-file-upload.appspot.com/")

# wait for the upload box to be visible
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[ngf-select]")))

# get a model
model = element.get_attribute("ng-model")

# find a file input by model
file_input = driver.find_element_by_css_selector('input[ngf-select][ng-model="%s"]' % model)

# upload an image
file_input.send_keys("/absolute/path/to/dr-evil-and-minion-laughing.png")

结果为:

enter image description here

关于python - 使用 Selenium 和 ng-file-upload 自动上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32590676/

相关文章:

python - 如何修复 "WebDriverException: Message: connection refused"?

Python pyodbc 奇怪的 unicode 字节码

python - 使 python 2.7.5 默认

angularjs - ag-Grid:尝试调用 sizeColumnsToFit() 但网格返回时宽度为零,也许网格在屏幕上还不可见?

java - 如何通过Selenium从文本节点中提取文本?

python - Headless_ie_driver 启动 Internet Explorer 时出现意外错误。 IELaunchURL() 返回 HRESULT 80070012 ('There are no more files.' )

python - 在 Windows 7 上设置 CMUSphinx/PocketSphinx

python - 在 Python 中使用原始文档字符串的部分函数应用程序?

angularjs - 如何做http post json数据

javascript - Angular Controller 阵列错误