excel 尝试根据I1的单元格值打开网站,按Tab键X次,然后按Enter键

dwbf0jvd  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(130)

不太擅长创建VBA代码,但尝试根据工作表的单元格I1中的URL通过Edge打开网站,sendkeys选项卡19次(因为我不知道如何选择网站的按钮,否则),点击回车,制表符再次34次,最后点击回车。这里是代码,当我试图调试,但它显示ActiveX不能创建对象。任何帮助将不胜感激,谢谢!
打开子网站并导航()

' 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.应用程序”)。

ncecgwcz

ncecgwcz1#

首先,我建议你使用Google Chrome代替Edge进行开发,因为它更适合开发人员。
您应该从下面的链接下载 selenium 的VBA和安装在您的系统中,请务必记下安装位置。
https://github.com/florentbr/SeleniumBasic/releases/latest
安装SeleniumBasic后,您可以在“C:\用户\您的姓名\AppData\Local\SeleniumBasic\Selenium.chm”中找到一些指南
您可能希望尝试文档中的WebElement.SendKeys Method部分。
然后从下面的链接https://chromedriver.chromium.org/downloads下载microsoft edge驱动程序
(make确保下载正确的驱动程序/版本为您的系统)下载驱动程序解压缩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

相关问题