如何使用selenium python上传图像?

2024-04-26 14:37:51 发布

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

我想上传一个图像到一个网站使用硒。你知道吗

我必须点击的按钮的HTML在下面的图片中(突出显示)

Upload Button Element

问题是当我点击这个元素时,屏幕上会弹出一个窗口来选择我想要上传的图像,这是我无法用Selenium控制的。 LIKE THIS

请告诉我如何从弹出窗口中选择图像,并尝试了发送密钥的方法,但我没有成功。你知道吗


Tags: 方法图像元素屏幕网站htmlselenium密钥
2条回答

从这个quesion,读:

Upload input control opens a native dialog (it is done by browser) so clicking on the control or browse button via Selenium will just pop the dialog and the test will hang.

所以,从here得到的解:

file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")
  1. 您需要通过cmd屏幕安装AutoIt“pip install-U pyautoit”

  2. 在脚本页上键入“import autoit”

  3. 在脚本中弹出“文件”对话框之前,请键入以下内容:

    autoit.win_active("Open") autoit.control_send("Open","Edit1",r"Path with filename") autoit.control_send("Open","Edit1","{ENTER}")

供您参考https://pypi.python.org/pypi/PyAutoIt/0.3

请注意:第3节将在点击按钮后实施。 希望这对你有帮助。你知道吗

相关问题 更多 >