配置自动化的浏览器.
查看该库所支持java的最低版本 here.
应熟练掌握build tool以安装支持java的Selenium库
Maven
具体的依赖位于项目中的 pom.xml
文件:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
Show full example
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.selenium</groupId>
<artifactId>selenium-examples</artifactId>
<version>1.0.0</version>
<properties>
<surefire.parallel>1</surefire.parallel>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>4.31.0</selenium.version>
</properties>
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-grid</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.titusfortner</groupId>
<artifactId>selenium-logger</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
</configurationParameters>
</properties>
<rerunFailingTestsCount>3</rerunFailingTestsCount>
</configuration>
</plugin>
</plugins>
</build>
</project>
Gradle
具体的依赖位于项目中的 build.gradle
文件中的 testImplementation
:
testImplementation 'org.seleniumhq.selenium:selenium-java:4.31.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.12.1'
Show full example
plugins {
id 'java'
}
group 'dev.selenium'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.seleniumhq.selenium:selenium-java:4.31.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.12.1'
}
test {
useJUnitPlatform()
}
该库所支持的Python版本最低版本可以在
支持的Python版本
章节中找到 PyPi
这里提供了几种不同的方式来安装 Selenium .
Pip
下载
此外你可以从这里下载 PyPI Built Distribution
(selenium-x.x.x.-py3-none-any.whl) 并通过: pip 文件安装:
pip install selenium-x.x.x.-py3-none-any.whl
在项目中使用
为了在项目中使用它,需要将它添加到 requirements.txt
文件中:
Show full example
selenium==4.31.0
pytest==8.3.5
trio==0.29.0
pytest-trio==0.8.0
pytest-rerunfailures==15.0
flake8==7.2.0
requests==2.32.3
Selenium 所支持的所有平台的列表一览
见诸于 Nuget
该处阐述了一些安装Selenium的选项.
包管理器
Install-Package Selenium.WebDriver
.NET CLI
dotnet add package Selenium.WebDriver
CSProj
在 csproj
文件里, 具体的依赖 PackageReference
(包参数) 位于 ItemGroup
(项目组)中:
<PackageReference Include="Selenium.WebDriver" Version="4.31.0" />
Show full example
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
<PackageReference Include="Selenium.Support" Version="4.31.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.31.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="LocalPackages" />
</ItemGroup>
</Project>
其他附加思虑事项
更多的注意事项,适用于使用 Visual Studio Code (vscode) 和 C#
安装兼容的 .NET SDK 作为章节的先决条件
同时安装 vscode 的扩展 (Ctrl-Shift-X)以适配 C# 和 NuGet
可以遵照此处进行 操作指南
创建 C# 控制台项目并运行 “Hello World”.
你也可以用命令行 dotnet new NUnit
创建NUnit初阶项目.
确保文件 %appdata%\NuGet\nuget.config
已经配置完成,就像某位开发者报告的问题一样,它可能因为某种因素被自动清空.
如果 nuget.config
是空的,或者未配置的,那么 .NET 创建的Selenium项目可能失败.
加入如下章节到文件 nuget.config
如果出现清空的情况:
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</packageSources>
...
更多关于 nuget.config
的信息 点击.
你可能需要按照自己的需求配置 nuget.config
.
现在,返回 vscode ,按下 Ctrl-Shift-P, 然后键入 “NuGet Add Package”, 并选择自己需要的 Selenium 包,例如 Selenium.WebDriver
.
按下回车并选择版本.
现在你可以使用说明文档中关于 C# vscode下的案例了.
你可以查看 Selenium 对 Ruby 版本支持和最低支持.
具体位于 rubygems.org
Selenium 可以使用两种不同方法安装.
手动安装
gem install selenium-webdriver
加入项目的 gemfile
gem 'selenium-devtools', '= 0.135.0'
Show full example
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'ffi', '~> 1.15', '>= 1.15.5' if Gem.win_platform? # Windows only
gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.35'
gem 'rubocop-rspec', '~> 3.0'
gem 'selenium-devtools', '= 0.135.0'
gem 'selenium-webdriver', '= 4.31.0'
You can find the minimum required version of Node for any given version of Selenium in the
你可以在此查看 Selenium 对 Node 的版本支持情况
位于 Node Support Policy
中的相关章节 npmjs
Selenium is typically installed using npm.
本地安装
npm install selenium-webdriver
加入项目
在你的项目 package.json
, 必须加入到 dependencies
:
Show full example
{
"name": "javascript-examples",
"version": "1.0.0",
"scripts": {
"test": "npx mocha test/**/*.spec.js --timeout 90000"
},
"author": "The Selenium project",
"license": "Apache-2.0",
"dependencies": {
"assert": "2.1.0",
"selenium-webdriver": "4.31.0"
},
"devDependencies": {
"mocha": "11.1.0"
}
}
Use the Java bindings for Kotlin.