【问题标题】:How to upload image with angular components using python selenium如何使用 python selenium 上传带有角度组件的图像
【发布时间】:2019-05-16 10:19:40
【问题描述】:

我的自动化脚本需要将图像上传到网页,但我似乎无法使用普通的 send_keys 方法上传图像。我怀疑它与 Angular 组件有关,但我不确定如何使用 Selenium webdriver 访问它们。

我尝试了以下方法: Automate file upload using Selenium and ng-file-uploadHow to upload file using python+selenium?

这些似乎没有我正在寻找的解决方案。

<button class="md-raised choose-file md-button md-ink-ripple ng-empty ng-valid" type="button" ng-transclude="" ngf-select="" accept="image/*" ng-model="vm.uploader.original" aria-invalid="false">Choose file</button>

我没有任何错误,因为我可以成功定位元素,但图片没有上传/发送。

file_input.send_keys("/location/of/image/profile_student.jpg")

【问题讨论】:

  • Choose file 按钮旁边有输入框吗?或者您正在点击Choose file 按钮及其弹出窗口?
  • @KunduK 不,我只有选择文件按钮。单击它后,它会打开“打开文件”弹出框。
  • 好的。要访问 windows 对象,您需要使用 autoit 和 selenium。

标签: python angularjs selenium selenium-webdriver browser-automation


【解决方案1】:

要处理 windows 对象,您可以使用autoit 但是有AutoItX3.dll 的 Python 绑定

你需要使用 pip 安装PyAutoIt

pip install -U pyautoit

您需要在您的 python 脚本中导入 autoit。

自动导入

先点击chose file按钮。

driver.find_element_by_xpath("//button[@type='button'][contains(.,'Choose file')]").click()

然后用relevant file path添加以下代码

filepath="C:\\filelocation\\filename.jpg"
autoit.win_wait_active("File Upload",5)
if autoit.win_exists("File Upload"):
   autoit.control_send("File Upload","Edit1",filepath+"{ENTER}")

如果您需要进一步的帮助,请告诉我。

【讨论】:

  • 这可以在mac上工作吗?只会在mac/linux环境下使用。
  • 我不使用 mac 或 Linux。但是值得一试。让我知道它是怎么回事?
  • 安装失败,经过一番研究,它似乎只适用于 Windows。
  • @LeroySharp : 看看你能不能安装pip install pypiwin32这个?
  • 使用 AutoIt 是一个 hack,selenium 已经支持文件上传,你只需要找到 元素并填充它。如果不存在,则需要使用 JavascriptExecutor 挂钩到 JavaScript 实现。你永远不应该使用 AutoIt,它很脆弱,不兼容跨平台并且只能在本地机器上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2016-10-19
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多