英文:
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()
' 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
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 "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论