尝试打开一个基于I1单元格数值的网站,然后按Tab键X次,然后按Enter键。

huangapple go评论80阅读模式
英文:

Trying to Open a Website based on I1's cell value and hit tab X number of times then hit enter

问题

以下是您提供的VBA代码的翻译部分:

Sub OpenWebsiteAndNavigate()

    ' 声明变量
    Dim edge As Object
    Dim url As String
    Dim i As Integer
    
    ' 从单元格I1获取URL
    url = Range("I1").Value
    
    ' 创建Microsoft Edge的新实例
    Set edge = CreateObject("Microsoft.Edge.Application")
    
    ' 导航到URL
    edge.navigate url
    
    ' 等待网站加载
    Do While edge.Busy Or edge.readyState <> 4
        DoEvents
    Loop
    
    ' 模拟按键操作以导航到网站上的特定元素
    For i = 1 To 19
        SendKeys "{TAB}"
    Next i
    SendKeys "{ENTER}"
    For i = 1 To 34
        SendKeys "{TAB}"
    Next i
    SendKeys "{ENTER}"
    
    ' 等待网站加载
    Do While edge.Busy Or edge.readyState <> 4
        DoEvents
    Loop

End Sub

这段代码尝试使用VBA在Microsoft Edge中打开一个网站,根据工作表中单元格I1中的URL,模拟按键操作以导航到特定元素。但是,您提到代码显示“ActiveX无法创建对象”的错误。这可能是由于Microsoft Edge不支持此种创建对象的方式,或者可能需要特定的设置。您可能需要尝试其他方法来自动化Edge浏览器的操作。

英文:

Not great at creating a VBA code but trying to open a website via Edge based on the url in cell I1 of the worksheet, sendkeys tab 19 times (as I cant figure out how to select the website's button otherwise), hit enter, tab again 34 times, and finally hit enter. Here is the code and when I try to debug, but its showing ActiveX can't create object. Any help would be much appreciated, thanks!

Sub OpenWebsiteAndNavigate()

&#39; Declare variables
Dim edge As Object
Dim url As String
Dim i As Integer

&#39; Get the URL from cell I1
url = Range(&quot;I1&quot;).Value

&#39; Create a new instance of Microsoft Edge
Set edge = CreateObject(&quot;Microsoft.Edge.Application&quot;)

&#39; Navigate to the URL
edge.navigate url

&#39; Wait for the website to load
Do While edge.Busy Or edge.readyState &lt;&gt; 4
    DoEvents
Loop

&#39; Simulate keystrokes to navigate to specific elements on the website
For i = 1 To 19
    SendKeys &quot;{TAB}&quot;
Next i
SendKeys &quot;{ENTER}&quot;
For i = 1 To 34
    SendKeys &quot;{TAB}&quot;
Next i
SendKeys &quot;{ENTER}&quot;

&#39; Wait for the website to load
Do While edge.Busy Or edge.readyState &lt;&gt; 4
    DoEvents
Loop

End Sub

Expected it load the website, cycle tab 19 times and hit enter, cycle 34 times and hit enter again but ActiveX cant create ("Microsoft.Edge.Application").

答案1

得分: 2

首先,我建议您在开发过程中使用Google Chrome而不是Edge,因为它更适合开发人员。

您应该从以下链接下载并安装VBA的Selenium,确保记下安装位置。

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

安装了SeleniumBasic后,您可以在此处找到一些指南
"C:\Users\Your Name\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 '最大化浏览器窗口

    bot.Get (Range("I1").Value) '这不是推荐的做法,您应该使用特定的引用与工作表名称,或使用范围对象

    bot.FindElementById ("someID") '根据ID查找元素
    bot.FindElementByXPath("//*[@id='loginBtn']").Click '根据XPath查找元素并点击
    bot.ExecuteScript ("一些JavaScript")

    bot.SendKeys ("一些内容") '在Web表单中输入一些内容

    '选择下拉框
    Set element = bot.FindElementByXPath("//*[@id='ddlModuleModal']")
    element.AsSelect.SelectByValue (110)

End Sub
英文:

First of all i suggest you to use, Google Chrome instead of Edge for development purpose. as it is more developer friendy.

You should download selenium for VBA from below link & install it in your system, make sure to note down the installation location.

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

after you have installed SeleniumBasic you can find some guide here
"C:\Users\Your Name\AppData\Local\SeleniumBasic\Selenium.chm"

You might want to try the WebElement.SendKeys Method section in the documentation.

Then download microsoft edge driver from the below link
https://chromedriver.chromium.org/downloads

(make sure to download the right driver/version for your system) download the driver unzip the exe file and put it in the SeleniumBasic installation folder.

*** Now in VBA window goto Tools > References > select Selenium Type Library you must do this step.

Sub OpenWebsiteAndNavigate()

    Dim bot As New WebDriver
    Dim element As Selenium.WebElement

    bot.Start &quot;chrome&quot;
    bot.Window.Maximize &#39; to maximize browser window

    bot.Get (Range(&quot;I1&quot;).Value)  &#39; this is not recomended you should use spacific referance with sheet name or use Range object

    bot.FindElementById (&quot;someID&quot;) &#39; to find some thing with ID
    bot.FindElementByXPath(&quot;//*[@id=&#39;loginBtn&#39;]&quot;).Click  &#39; to find some thing with xpath
    bot.ExecuteScript (&quot;some java script&quot;)

    bot.SendKeys (&quot;someThing&quot;) &#39; to right some thing in web form

    &#39;selects drop down
    Set element = bot.FindElementByXPath(&quot;//*[@id=&#39;ddlModuleModal&#39;]&quot;)
    element.AsSelect.SelectByValue (110)

End Sub

huangapple
  • 本文由 发表于 2023年3月4日 05:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定