7种编程语言调用Chrome浏览器的最佳实践:Chrome-Selenium

Selenium是一种很成熟的浏览器自动化解决方法,支持各种编程语言访问各种主流浏览器。

Selenium技术比较麻烦的一个问题是,浏览器的自动更新引起程序不能正确运行,浏览器版本升级以后,开发人员或用户必须重新下载驱动文件,才能保证Selenium程序正常运行。

 

 

 

 

 

由于Chrome是用户最多的浏览器,博主于2022/10/29推出Chrome-Selenium,用户只需要下载一个压缩包,就具备了Selenium开发的条件,该压缩包内容包括:

  • 免安装、并且不更新的Chrome浏览器
  • 驱动文件chromedriver.exe
  • SeleniumBasic
  • .NET语言需要的WebDriver.dll等

该解决方案具有如下几个优点:

一、无论你的电脑之前有没有安装Chrome,下载这个压缩包无需安装,就可以使用Chrome

二、以后再也不需要寻找驱动

三、压缩包中带有作者赠送的如下7个示例源代码:

  C#、VB.NET、PowerShell

  VBA、VB6、VBS【这3个需要管理员运行regasm.bat生成tlb文件】

  Python【需要事先用 pip install selenium安装模块】

 

Chrome-Selenium.zip下载方法: 

链接:https://pan.baidu.com/s/1wtNFWWLzdkWyNk_7kN-MTg
提取码:1234

使用方法:

下载以后,假设解压到了D盘,可以看到里面有2个文件夹和一些可执行文件。

 其中,Chrome-bin文件夹下面有一个Chrome.exe文件,这个就是浏览器的执行文件。

Example文件夹则是我写的各种语言的项目,用户可以打开这些工程查看代码进行调试。

Chrome-Selenium文件夹下面的这些exe和dll文件,则是Selenium开发必需的支持文件。 

总结:

Chrome-Selenium.zip给开发人员节省了如下工作量:

  • 下载和安装Chrome
  • 查看版本号、寻找对应驱动
  • 项目引用文件

由于作者赠送了7种语言的经典代码,开发人员不需要查看太多资料即可看到浏览器启动然后退出的过程。 

C#版:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OpenQA.Selenium; //Selenium命名空间
using OpenQA.Selenium.Chrome; //Chrome命名空间

namespace Selenium_CS
{
    public partial class Form1 : Form
    {
        private IWebDriver WD;
        public Form1()
        {
            InitializeComponent();
        }

        //已经为项目添加引用:"D:\Chrome-Selenium\WebDriver.dll"
        //已经为项目添加引用:"D:\Chrome-Selenium\WebDriver.Support.dll"
        private void button1_Click(object sender, EventArgs e)
        {
            ChromeOptions Options = new ChromeOptions();
            Options.BinaryLocation = @"D:\Chrome-Selenium\Chrome-bin\chrome.exe"; //指定Chrome.exe的位置
            ChromeDriverService Service = ChromeDriverService.CreateDefaultService(driverPath: @"D:\Chrome-Selenium", driverExecutableFileName: "chromedriver.exe");
            //指定chromedriver.exe的位置和文件名
            Service.HideCommandPromptWindow = false; //是否隐藏黑色窗口
            WD = new ChromeDriver(options: Options, service: Service);
            WD.Url = "https://www.baidu.com/";
            IWebElement Keyword = WD.FindElement(By.Id("kw"));
            IWebElement Submit = WD.FindElement(By.Id("su")); //百度一下按钮
            Keyword.SendKeys("刘永富的博客园");
            Submit.Click();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WD.Quit();
        }
    }
}

 

VB.NET版

'已经为项目添加引用:"D:\Chrome-Selenium\WebDriver.dll"
'已经为项目添加引用:"D:\Chrome-Selenium\WebDriver.Support.dll"

Imports OpenQA.Selenium 'Selenium命名空间
Imports OpenQA.Selenium.Chrome 'Chrome命名空间

Public Class Form1
    Private WD As IWebDriver
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Options As ChromeOptions = New ChromeOptions()
        Options.BinaryLocation = "D:\Chrome-Selenium\Chrome-bin\chrome.exe" '指定Chrome浏览器位置
        Dim Service As ChromeDriverService = ChromeDriverService.CreateDefaultService(driverPath:="D:\Chrome-Selenium", driverExecutableFileName:="chromedriver.exe")
        '指定驱动文件位置
        Service.HideCommandPromptWindow = False
        WD = New ChromeDriver(options:=Options, service:=Service)
        WD.Url = "https://www.baidu.com/"
        Dim Keyword As IWebElement = WD.FindElement(By.Id("kw"))
        Dim Submit As IWebElement = WD.FindElement(By.Id("su"))
        Keyword.SendKeys("刘永富的博客园")
        Submit.Click()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        WD.Quit()
    End Sub
End Class

PowerShell版

Add-Type -Path "D:\Chrome-Selenium\WebDriver.dll"
Add-Type -Path "D:\Chrome-Selenium\WebDriver.Support.dll"
[OpenQA.Selenium.Chrome.ChromeOptions]$Options=New-Object OpenQA.Selenium.Chrome.ChromeOptions
$Options.BinaryLocation ="D:\Chrome-Selenium\Chrome-bin\chrome.exe" #指定Chrome.exe的位置
[OpenQA.Selenium.Chrome.ChromeDriverService]$Service = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService("D:\Chrome-Selenium")
$Service.HideCommandPromptWindow=$true #是否隐藏黑窗
[OpenQA.Selenium.Chrome.ChromeDriver]$WD=New-Object OpenQA.Selenium.Chrome.ChromeDriver($Service,$Options)
[OpenQA.Selenium.INavigation]$Nav=$WD.Navigate()
$Nav.GoToUrl("https://www.baidu.com/")
Start-Sleep -Seconds 5
$WD.FindElementById("kw").SendKeys("刘永富的博客园")
Start-Sleep -Seconds 5
[OpenQA.Selenium.IWebElement]$BaiduYixia = $WD.FindElementById("su")
$BaiduYixia.Click()
Write-Host $WD.Url $WD.Title
Start-Sleep -Seconds 5
$WD.Quit()

VBA/VB6版

'当前工程添加了引用 "D:\Chrome-Selenium\SeleniumBasic.tlb"
Option Explicit
Private WD As SeleniumBasic.IWebDriver

Sub Command1_Click()
    On Error GoTo Err1
    Dim Service As SeleniumBasic.ChromeDriverService
    Dim Options As SeleniumBasic.ChromeOptions
    Set WD = New SeleniumBasic.IWebDriver
    Set Service = New SeleniumBasic.ChromeDriverService
    With Service
        .CreateDefaultService driverPath:="D:\Chrome-Selenium"
        .HideCommandPromptWindow = True
    End With
    Set Options = New SeleniumBasic.ChromeOptions
    With Options
        .BinaryLocation = "D:\Chrome-Selenium\Chrome-bin\chrome.exe"
    End With
    WD.New_ChromeDriver Service:=Service, Options:=Options
    WD.url = "https://www.baidu.com"
    Dim keyword As SeleniumBasic.IWebElement
    Dim button As SeleniumBasic.IWebElement
    Set keyword = WD.FindElementById("kw")
    keyword.Clear
    keyword.SendKeys "刘永富的博客园"
    Set button = WD.FindElementById("su")
    button.Click
    Debug.Print WD.Title, WD.url
    Exit Sub
Err1:
    MsgBox Err.Description, vbCritical
End Sub

Sub Command2_Click()
    WD.Quit
End Sub

VBS版

'后期绑定
Private WD
Call Baidu()
Sub Baidu()
    Dim Service
    Dim Options    
    Set Service = CreateObject("SeleniumBasic.ChromeDriverService")
    With Service
        .CreateDefaultService "D:\Chrome-Selenium"
        .HideCommandPromptWindow = True
    End With
    Set Options = CreateObject("SeleniumBasic.ChromeOptions")
    With Options
        .BinaryLocation = "D:\Chrome-Selenium\Chrome-bin\chrome.exe"
    End With
    Set WD = CreateObject("SeleniumBasic.IWebDriver")
    WD.New_ChromeDriver
    WD.URL = "https://www.baidu.com"
    Dim keyword
    Dim button
    Set keyword = WD.FindElementById("kw")
    keyword.Clear
    keyword.SendKeys "刘永富的博客园"
    Set button = WD.FindElementById("su")
    button.Click
    MsgBox "退出浏览器"
    WD.Quit
End Sub

 

Python版

#请使用 pip install selenium 安装selenium模块
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

if __name__=='__main__':
    O=Options()
    O.binary_location=r"D:\Chrome-Selenium\Chrome-bin\chrome.exe"
    S =Service(r"D:\Chrome-Selenium\chromedriver.exe")
    WD=webdriver.Chrome(service=S,options=O)
    WD.get("https://www.baidu.com/")
    keyword = WD.find_element(By.ID,"kw")
    keyword.send_keys("刘永富的博客园")
    button =WD.find_element(By.ID,"su")
    button.click()
    time.sleep(5)
    WD.quit()

上述分享内容完全免费。如果觉得博主写得好,欢迎各位读者扫描如下二维码赞助(多多益善)。

 

posted @ 2022-10-29 21:34  ryueifu  阅读(3624)  评论(0编辑  收藏  举报