Selenium VBA:填充 Windows 弹出窗口以上传文件 [英] Selenium VBA: Fill a Windows Popup to upload a file

查看:64
本文介绍了Selenium VBA:填充 Windows 弹出窗口以上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Chrome 浏览器的原因,我无法与 Windows PopUp 交互以编写使用 Selenium 在 VBA 中上传文件的专用路径.

I'm not able to have interaction with Windows PopUp to write a dedicated path to upload a file in VBA using Selenium due to Chrome browser.

我在 Python 中找到了一些解决方案,但在 VBA 中没有找到.这就是我尝试在 VBA 中转换代码但没有成功的原因.

I have found some solutions in Python but nothing in VBA. This is why I have tried to convert the code in VBA but without success.

在下面找到示例代码:

Sub Fullfil_Windows_PopUp()
Dim driver
Dim elem

    Set Waiter = CreateObject("Selenium.Waiter")
    Set Assert = CreateObject("Selenium.Assert")
    Set driver = CreateObject("Selenium.ChromeDriver")

    'open the browser and the page
    driver.Get "https://fr.imgbb.com/"
    While Waiter.Not(InStr(driver.Title, "ImgBB — Upload Image — Hébergement d'images gratuit")): Wend

    'open upload window
    Set elem = driver.FindElementsByXPath("//*[@id='home-cover-content']/div[2]/a").Item(1)
    elem.Click

    'Trial 1
    driver.SwitchToWindowByTitle "Ouvrir" '--> NOK (error window not found)
    driver.SendKeys "D:\Test.jpg"

    'Trial 2
    driver.SwitchToAlert.SendKeys "D:\Test.jpg" '--> NOK (error No Alert present)


    'Trial 3
    Set wsh = CreateObject("WScript.Shell")
    wsh.SendKeys "D:\Test.jpg" '--> NOK (no action)


End Sub

预先感谢您的建议

推荐答案

我使用了后门"将文件名填充到文件管理器窗口中.由于光标已经进入窗口的文件名字段,我在 VBS 中创建了一个外部脚本来写入文件名(以及 2x TAB + ENTER 以验证窗口).

I have used a "back door" to fill the file name into the filemanager window. As the cursor is already into the filename field of the window, I have created an external script in VBS to write the file name (and 2x TAB + ENTER to validate the window).

VBS文件代码下方:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "D:\Test.jpg"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"

我在用 selenium 打开文件管理器窗口之后执行这个脚本:

And I execute this script after to have open the filemanager window with selenium:

Sub Fullfil_Windows_PopUp()
Dim driver
Dim elem

    ' Chrome browser
    Set driver = CreateObject("Selenium.ChromeDriver")

    'open the browser and the page
    driver.Get "https://fr.imgbb.com/"
    Sleep 1000

    ' Open upload window
    Set elem = driver.FindElementsByXPath("//*[@id='home-cover-content']/div[2]/a").Item(1)
    elem.Click

    ' Write the filename with VBS script
    Set oWsh = CreateObject("Shell.Application")
    oWsh.ShellExecute "D:\filename.vbs"
    Set oWsh = Nothing
    Sleep 1500

End Sub

但我还是继续直接用Selenium搜索解决方案,因为比如直接在VBS文件中指明文件名,这不像变体那样好用.

But I continue to search a solution directly with Selenium because for example, the filename is directly indicated into the VBS File and this is not easy to use it like a variant.

这篇关于Selenium VBA:填充 Windows 弹出窗口以上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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