Selenium + VBA控制Chrome [英] Selenium + VBA to Control Chrome

查看:7358
本文介绍了Selenium + VBA控制Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用selenium + vba启动Chrome,打开10个列在单元格范围内的网址(A1:A10)。
我对硒并不熟悉,经过多次尝试后,终于出现了笨重的代码。

  Private selenium作为新的ChromeDriver 

Sub test()
Dim cell As Range
Dim keys As New selenium.keys
Dim pageNo As Integer

pageNo = 0
selenium.Startchrome,http://www.google.com/
对于单元格In Range(A1:A10)
如果pageNo> ; = 1然后
selenium.SendKeys keys.Control& t+ keys.Control& t
selenium.SwitchToNextWindow
End If
selenium.Get cell.Value
pageNo = pageNo + 1
Next
End Sub

提出了一些麻烦和问题:
$ b


  1. 我很困惑为什么我必须发送2次Ctrl + t键才能成功打开新标签。

  2. 为什么我必须添加selenium.SwitchToNextWindow在打开新标签之后,还是下一个url会在原有标签上打开,而不是新标签? 常常在打开3 〜5个网址,vba弹出没有足够的内存错误,并停止加载下一个网址。但是如果我在调试环境中逐行运行代码,所有10个URL都可以成功打开。为什么会这样?
  3. 我搜索了一下,如果我想要chrome在完成运行宏后不能退出,我不得不在sub之外声明对象selenium得到原因。任何人都可以用简单的方式解释我的意见吗?


如果有专家可以帮我优化我的代码,不胜感激!我的系统是Win7 64bit + Excel 2010。



非常感谢。

解决方案


我很困惑,为什么我必须发送两次Ctrl + t键才能成功打开新标签。

修饰键应该在第一个参数中,而键在第二个中:

 驱动程序。 SendKeys Keys.Control,t

在你的情况中,更好的方法是打开一个新的一些Javascript的窗口:

 私钥作为新selenium.Keys 
私有驱动程序作为新selenium.ChromeDriver

Sub test()
Const JS_NEW_WINDOW =window.open(arguments [0],name);

driver.Gethttp://www.google.com/
For i = 0 To 9
If if Then
driver.ExecuteScript JS_NEW_WINDOW, http://www.google.com/
driver.SwitchToNextWindow
driver.FindElementById(lst-ib)。SendKeyssome text& i
End If
Next
End Sub




为什么我必须添加selenium.SwitchToNextWindow在打开新标签之后,或者下一个url会在原始标签上打开,而不是新标签?

打开新窗口时,驱动程序的上下文保持不变。你必须明确地告诉驱动程序你想在另一个窗口上操作。


打开3〜5个URL后,vba弹出内存不足并停止加载下一个网址。但是如果我在调试环境中逐行运行代码,所有10个URL都可以成功打开。为什么会这样?


可能是因为某些内存在页面完全加载后才会释放。但是,由于您要连续加载所有页面,它可能不会让浏览器有足够的时间来管理其内存。
我会尝试禁用插件(如果有的话)(特别是Flash),并添加一些等待:

  Dim drivers As New Selenium.ChromeDriver 
driver.SetPreferenceplugins.plugins_disabled,Array(Adobe Flash Player)
For i = 0至9
driver.Gethttp://www.google。 com /
Waiter.wait 500'等待500ms
下一个




我搜索了一下,如果我想要chrome在完成宏运行后不能退出,我不得不在sub之外声明对象selenium,尽管我不太明白原因。任何人都可以用简单的方式帮我解释一下吗?


一旦持有驱动程序的变量不再使用,驱动程序会自动终止。
选中此链接以获取有关变量和作用域的更多信息: https:// support .microsoft.com / en-gb / kb / 141693
因此,为了让驱动程序在程序外保持活跃状态​​,必须在外部声明:

  Dim驱动程序为Selenium。 ChromeDriver 

Sub Main
Set driver =新增Selenium.ChromeDriver
driver.Gethttp://www.google.com/

结束Sub


I use selenium + vba to launch chrome to open 10 urls listed in cells range("A1:A10"). I am not familiar with selenium, after trying a lot of times I finally come out below clunky codes.

Private selenium As New ChromeDriver

Sub test()
Dim cell As Range
Dim keys As New selenium.keys
Dim pageNo As Integer

    pageNo = 0
    selenium.Start "chrome", "http://www.google.com/"
    For Each cell In Range("A1:A10")
        If pageNo >= 1 Then
            selenium.SendKeys keys.Control & "t" + keys.Control & "t"
            selenium.SwitchToNextWindow
        End If
        selenium.Get cell.Value
        pageNo = pageNo + 1
    Next
End Sub

Several troubles and questions raised:

  1. I am confused why I have to send 2 times of "Ctrl+t" keys to successfully open new tab.

  2. Why that I have to add selenium.SwitchToNextWindow after opening new tab, or the next url will be opened on the original tab, not new tab?

  3. Quite often after opening 3~5 urls, vba pops up "not enough memory" error and stops loading the next url. But if I run the codes line by line in debugging environment, all 10 urls can be successfully opened. Why's that?

  4. I googled that if I want chrome not to exit after finishing running macro, I have to declare object selenium outside the sub though I do not quite get the reason. Could anyone help explain me in plain way?

If any experts can help me optimize my codes, that would be much appreciated! My system is Win7 64bit + Excel 2010.

Thanks with best regards.

解决方案

I am confused why I have to send 2 times of "Ctrl+t" keys to successfully open new tab.

The modifier key should be in the first argument and the key in the second one:

driver.SendKeys Keys.Control, "t"

In your case, a better way would be to open a new window with some Javascript:

Private Keys As New selenium.Keys
Private driver As New selenium.ChromeDriver

Sub test()
  Const JS_NEW_WINDOW = "window.open(arguments[0], name);"

  driver.Get "http://www.google.com/"
  For i = 0 To 9
    If i Then
      driver.ExecuteScript JS_NEW_WINDOW, "http://www.google.com/"
      driver.SwitchToNextWindow
      driver.FindElementById("lst-ib").SendKeys "some text " & i
    End If
  Next
End Sub

Why that I have to add selenium.SwitchToNextWindow after opening new tab, or the next url will be opened on the original tab, not new tab?

The context of the driver remains the same when a new window is opened. You have to explicitly tell the driver that you want to operate on another window.

Quite often after opening 3~5 urls, vba pops up "not enough memory" error and stops loading the next url. But if I run the codes line by line in debugging environment, all 10 urls can be successfully opened. Why's that?

Probably because some of the memory is released once the page is fully loaded. But since you are loading all the pages in a row, it may not leave the browser enough time to manage it's memory. I would try to disable the plugins if any (especially Flash) and add some waiting:

Dim driver As New Selenium.ChromeDriver
driver.SetPreference "plugins.plugins_disabled", Array("Adobe Flash Player")
For i = 0 To 9
  driver.Get "http://www.google.com/"
  Waiter.wait 500  ' waits 500ms
Next

I googled that if I want chrome not to exit after finishing running macro, I have to declare object selenium outside the sub though I do not quite get the reason. Could anyone help explain me in plain way?

The driver is automatically terminated once the variable holding the driver is no longer used. Check this link to get more information about variable and scopes: https://support.microsoft.com/en-gb/kb/141693 . So to keep the driver alive outside of a procedure, it must be declared outside:

Dim driver As Selenium.ChromeDriver

Sub Main
  Set driver = New Selenium.ChromeDriver
  driver.Get "http://www.google.com/"

End Sub

这篇关于Selenium + VBA控制Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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