Mozilla Firefox,中文俗称火狐浏览器,使用Gecko引擎。
- Firefox浏览器的下载
在其他浏览器中打开上述URL,点击“立即下载”按钮。
下载Firefox-latest.exe可执行文件,双击并安装。
安装结束后,默认安装位置位于:
C:Program FilesMozilla Firefox
手工启动Firefox浏览器,点击菜单“帮助”、“关于Firefox”
可以看到版本号是67.0.4(32位)。
看清楚版本号以后,就可以搜索与之匹配的驱动文件了。
- 驱动文件的下载
Firefox浏览器的驱动文件是geckodriver.exe,下载地址位于:
https://github.com/mozilla/geckodriver/releases
可以看到最新版是v0.26.0,向下滚动到Assets中,下载与Firefox版本适合的驱动文件。
解压到E:SeleniumDrivers路径下。
- 启动浏览器
示例代码:
Private WD As SeleniumBasic.IWebDriver Sub Baidu() On Error GoTo Err1 Dim Service As SeleniumBasic.FirefoxDriverService Dim Options As SeleniumBasic.FirefoxOptions Set WD = New SeleniumBasic.IWebDriver Set Service = New SeleniumBasic.FirefoxDriverService With Service .CreateDefaultService driverPath:="E:SeleniumDrivers" .HideCommandPromptWindow = True End With Set Options = New SeleniumBasic.FirefoxOptions With Options .BrowserExecutableLocation = "C:Program FilesMozilla Firefoxfirefox.exe" End With WD.New_FirefoxDriver Service:=Service, Options:=Options WD.URL = "https://www.baidu.com" Dim form As SeleniumBasic.IWebElement Dim keyword As SeleniumBasic.IWebElement Dim button As SeleniumBasic.IWebElement Set form = WD.FindElementById("form") Set keyword = form.FindElementById("kw") keyword.Clear keyword.SendKeys "好看视频" Set button = form.FindElementById("su") button.Click Debug.Print WD.Title, WD.URL Debug.Print WD.PageSource MsgBox "下面退出浏览器。" WD.Quit Exit Sub Err1: MsgBox Err.Description, vbCritical End Sub Sub Music() On Error GoTo Err1 Dim Service As SeleniumBasic.FirefoxDriverService Dim Options As SeleniumBasic.FirefoxOptions Dim Actions As SeleniumBasic.Actions Dim Utility As SeleniumBasic.Utility Dim Keys As SeleniumBasic.Keys Dim i As Integer Set WD = New SeleniumBasic.IWebDriver Set Utility = New SeleniumBasic.Utility Set Actions = New SeleniumBasic.Actions Set Keys = New SeleniumBasic.Keys Set Service = New SeleniumBasic.FirefoxDriverService With Service .CreateDefaultService driverPath:="E:SeleniumDrivers" .HideCommandPromptWindow = True End With Set Options = New SeleniumBasic.FirefoxOptions With Options .BrowserExecutableLocation = "C:Program FilesMozilla Firefoxfirefox.exe" End With WD.New_FirefoxDriver Service:=Service, Options:=Options WD.URL = "https://music.163.com/#" WD.Manage.Timeouts.PageLoad = 59 Utility.Sleep 3000 Dim search As SeleniumBasic.IWebElement Dim iframe As IWebElement Dim items() As SeleniumBasic.IWebElement Dim a As SeleniumBasic.IWebElement Dim item As SeleniumBasic.IWebElement Dim result As Variant Dim lockicon As SeleniumBasic.IWebElement Dim prv As SeleniumBasic.IWebElement Dim play As SeleniumBasic.IWebElement Set search = WD.FindElementById("srch") If search Is Nothing = False Then search.Clear Utility.Sleep 1000 search.SendKeys "周华健" Utility.Sleep 1000 search.SendKeys Keys.Enter Utility.Sleep 1000 Set iframe = WD.FindElementById("g_iframe") WD.SwitchTo.Frame iframe Set a = WD.FindElementByLinkText("单曲") a.Click Utility.Sleep 1000 items = WD.FindElementsByCssSelector("[class='opt hshow']") For i = 0 To UBound(items) Set item = items(i) result = WD.ExecuteScript("arguments[0].scrollIntoView(true);", item) Set Actions = New SeleniumBasic.Actions Actions.Create WD Utility.Sleep 1000 Actions.MoveToElement(item).Perform Utility.Sleep 1000 item.FindElementByTagName("a").Click Next i WD.SwitchTo.DefaultContent Set lockicon = WD.FindElementByClassName("btn") lockicon.Click Utility.Sleep 1000 Dim div As SeleniumBasic.IWebElement Dim buttons() As SeleniumBasic.IWebElement Set div = WD.FindElementById("g_player").FindElementByClassName("btns") buttons = div.FindElementsByTagName("a") Utility.Sleep 1000 buttons(0).Click buttons(2).Click If buttons(1).GetAttribute("data-action") = "play" Then buttons(1).Click End If End If Stop WD.Quit Exit Sub Err1: Stop Resume End Sub
- 运行截图