尝试根据 I1 的单元格值打开网站并点击选项卡 X 次然后点击回车

问题描述 投票:0回答:1

不擅长创建 VBA 代码,但尝试根据工作表单元格 I1 中的 url 通过 Edge 打开网站,sendkeys 选项卡 19 次(因为我无法弄清楚如何选择网站按钮),按回车键,选项卡又是 34 次,最后按回车键。这是代码,当我尝试调试时,它显示 ActiveX 无法创建对象。任何帮助将不胜感激,谢谢!

子 OpenWebsiteAndNavigate()

' Declare variables
Dim edge As Object
Dim url As String
Dim i As Integer

' Get the URL from cell I1
url = Range("I1").Value

' Create a new instance of Microsoft Edge
Set edge = CreateObject("Microsoft.Edge.Application")

' Navigate to the URL
edge.navigate url

' Wait for the website to load
Do While edge.Busy Or edge.readyState <> 4
    DoEvents
Loop

' Simulate keystrokes to navigate to specific elements on the website
For i = 1 To 19
    SendKeys "{TAB}"
Next i
SendKeys "{ENTER}"
For i = 1 To 34
    SendKeys "{TAB}"
Next i
SendKeys "{ENTER}"

' Wait for the website to load
Do While edge.Busy Or edge.readyState <> 4
    DoEvents
Loop

结束子

预期它会加载网站,循环选项卡 19 次并按回车键,循环 34 次并再次按回车键,但 ActiveX 无法创建(“Microsoft.Edge.Application”)。

excel vba microsoft-edge
1个回答
0
投票

首先,我建议您使用

Google Chrome
而不是 Edge 用于开发目的。因为它对开发人员更友好。

您应该从下面的链接下载用于 VBA 的 selenium 并将其安装到您的系统中,确保记下安装位置。

https://github.com/florentbr/SeleniumBasic/releases/latest

安装 SeleniumBasic 后,您可以在此处找到一些指南 “C:\Users\你的名字\AppData\Local\SeleniumBasic\Selenium.chm”

您可能想尝试文档中的

WebElement.SendKeys Method
部分。

然后从以下链接下载 Microsoft Edge 驱动程序 https://chromedriver.chromium.org/downloads

(确保为您的系统下载正确的驱动程序/版本)下载驱动程序解压缩 exe 文件并将其放入

SeleniumBasic
安装文件夹中。

*** 现在在 VBA 窗口中转到工具 > 参考 > 选择

Selenium Type Library
你必须执行此步骤。

Sub OpenWebsiteAndNavigate()

    Dim bot As New WebDriver
    Dim element As Selenium.WebElement

    bot.Start "chrome"
    bot.Window.Maximize ' to maximize browser window

    bot.Get (Range("I1").Value)  ' this is not recomended you should use spacific referance with sheet name or use Range object

    bot.FindElementById ("someID") ' to find some thing with ID
    bot.FindElementByXPath("//*[@id='loginBtn']").Click  ' to find some thing with xpath
    bot.ExecuteScript ("some java script")

    bot.SendKeys ("someThing") ' to right some thing in web form

    'selects drop down
    Set element = bot.FindElementByXPath("//*[@id='ddlModuleModal']")
    element.AsSelect.SelectByValue (110)

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.