入門 
Seleniumを初めて使用する場合は、すぐに習得するのに役立つリソースがいくつかあります。
    
      Seleniumは市場で主要なブラウザの全てを WebDriver  を使うことでサポートしています。
WebDriverとはAPI群とプロトコルです。これらはウェブブラウザの動作をコントロールするための言語中立なインターフェイスを定義しています。
それぞれのブラウザは特定のWebDriverの実装を持っており、これらは driver  と呼ばれます。
driverはブラウザに委譲する責務を持つコンポーネントであり、Seleniumとブラウザ間の通信を処理します。
この分離は、ブラウザベンダーに自分たちのブラウザでの実装の責任を持たせるための意図的な努力のひとつです。
Seleniumは可能な場合これらのサードパーティ製のdriverを使いますが、それが現実的でない場合のためにプロジェクトでメンテナンスしているdriverも提供しています。
Seleniumフレームワークはこれら全ての要素をユーザ向けのインターフェイスを通して結びつけます。このインターフェイスは異なるブラウザバックエンドを透過的に使えるようにし、クロスブラウザ・クロスプラットフォームの自動化を可能にします。
Selenium setup is quite different from the setup of other commercial tools.
Before you can start writing Selenium code, you have to
install the language bindings libraries for your language of choice, the browser you
want to use, and the driver for that browser.
Follow the links below to get up and going with Selenium WebDriver. 
If you wish to start with a low-code/record and playback tool, please check
Selenium IDE 
Once you get things working, if you want to scale up your tests, check out the
Selenium Grid .
 
 
  
  
  
  
  
  
  
    
    
	
    
    
	1 - Seleniumライブラリのインストール 
    お気に入りのプログラミング言語用にSeleniumライブラリを設定します。
	最初にあなたの自動化プロジェクトにSeleniumのバインディングをインストールする必要があります。
インストールの方法は選択した言語によって異なります。
Requirements by language 
  
  
  
  
  
  
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        View the minimum supported Java version here .
Installation of Selenium libraries for Java is accomplished using a build tool.
Maven 
Specify the dependency in the project’s pom.xml file:
          <dependency> 
             <groupId> org.seleniumhq.selenium</groupId> 
             <artifactId> selenium-java</artifactId> 
             <version> 4.9.1</version> 
         </dependency>   
  
Gradle 
Specify the dependency in the project build.gradle file as testImplementation:
      testImplementation  'org.seleniumhq.selenium:selenium-java:4.9.1'   
  
     
    
        The minimum supported Python version for each Selenium version can be found
in Supported Python Versions on PyPi 
There are a couple different ways to install Selenium.
Pip 
Download 
Alternatively you can download the PyPI source archive 
(selenium-x.x.x.tar.gz) and install it using setup.py :
Require in project 
To use it in a project, add it to the requirements.txt file:
  
  
     
    
        A list of all supported frameworks for each version of Selenium
is available on Nuget 
There are a few options for installing Selenium.
Packet Manager 
Install-Package Selenium.WebDriver
 .NET CLI 
dotnet add package Selenium.WebDriver
 CSProj 
in the project’s csproj file, specify the dependency as a PackageReference in ItemGroup:
        <PackageReference  Include= "Selenium.WebDriver"  Version= "4.9.0"  />   
  
Additional considerations 
Further items of note for using Visual Studio Code (vscode) and C#
Install the compatible .NET SDK as per the section above.
Also install the vscode extensions (Ctrl-Shift-X) for C# and NuGet.
Follow the instruction here 
to create and run the “Hello World” console project using C#.
You may also create a NUnit starter project using the command line dotnet new NUnit.
Make sure the file %appdata%\NuGet\nuget.config is configured properly as some developers reported that it will be empty due to some issues.
If nuget.config is empty, or not configured properly, then .NET builds will fail for Selenium Projects.
Add the following section to the file nuget.config if it is empty:
<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>
...
For more info about nuget.config click here .
You may have to customize nuget.config to meet you needs.
Now, go back to vscode, press Ctrl-Shift-P, and type “NuGet Add Package”, and enter the required Selenium packages such as Selenium.WebDriver.
Press Enter and select the version.
Now you can use the examples in the documentation related to C# with vscode.
     
    
        You can see the minimum required version of Ruby for any given Selenium version
on rubygems.org 
Selenium can be installed two different ways.
Install manually 
gem install selenium-webdriver
 Add to project’s gemfile 
  gem  'selenium-webdriver' ,  '= 4.9.1'  
  
     
    
        You can find the minimum required version of Node for any given version of Selenium in the
Node Support Policy section on npmjs 
Selenium is typically installed using npm.
Install locally 
npm install selenium-webdriver
 Add to project 
In your project’s package.json, add requirement to dependencies:
          "selenium-webdriver" :  "^4.9.2"   
  
     
    
        
    Use the Java bindings for Kotlin.
  
    
 
Next Step 
Create your first Selenium script 
 
    
	
  
    
    
	
    
    
	2 - 最初のSeleniumスクリプトを書く 
    Seleniumスクリプトを作成するための段階的な説明
	Seleniumをインストール し、
すると、Seleniumコードを書く準備が整います。
Note : if you get an error about drivers not found, please read about troubleshooting the
driver location error 
Eight Basic Components 
Seleniumが行うことはすべて、ブラウザコマンドを送信して、何かを実行したり、情報の要求を送信したりすることです。
Seleniumで行うことのほとんどは、次の基本的なコマンドの組み合わせです。
1. ドライバーインスタンスでセッションを開始します 
For more details on starting a session read our documentation on  driver sessions 
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          WebDriver  driver  =  new  ChromeDriver ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver  =  webdriver . Chrome ()   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              IWebDriver  driver  =  new  ChromeDriver ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver  =  Selenium :: WebDriver . for  :chrome   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build ();   
  
     
    
 
2. Take action on browser 
In this example we are ブラウザがナビゲート するコマンドを送信します
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        await  driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' );   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )   
  
     
 
There are a bunch of types of information about the browser  you
can request, including window handles, browser size / position, cookies, alerts, etc.
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          String  title  =  driver . getTitle ();   
  
     
    
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              var  title  =  driver . Title ;   
  
     
    
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        let  title  =  await  driver . getTitle ();   
  
     
    
 
4. Establish Waiting Strategy 
Synchronizing the code with the current state of the browser is one of the biggest challenges
with Selenium, and doing it well is an advanced topic.
Essentially you want to make sure that the element is on the page before you attempt to locate it
and the element is in an interactable state before you attempt to interact with it.
An implicit wait is rarely the best solution, but it’s the easiest to demonstrate here, so
we’ll use it as a placeholder.
Read more about Waiting strategies .
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver . implicitly_wait ( 0.5 )   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              driver . Manage (). Timeouts (). ImplicitWait  =  TimeSpan . FromMilliseconds ( 500 );   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      driver . manage . timeouts . implicit_wait  =  500   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        await  driver . manage (). setTimeouts ({ implicit :  500 });   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))   
  
     
 
5. 要素を検索する ためのコマンドを送信します 
The majority of commands in most Selenium sessions are element related, and you can’t interact
with one without first finding an element 
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          WebElement  textBox  =  driver . findElement ( By . name ( "my-text" )); 
         WebElement  submitButton  =  driver . findElement ( By . cssSelector ( "button" ));   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      text_box  =  driver . find_element ( by = By . NAME ,  value = "my-text" ) 
     submit_button  =  driver . find_element ( by = By . CSS_SELECTOR ,  value = "button" )   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              var  textBox  =  driver . FindElement ( By . Name ( "my-text" )); 
             var  submitButton  =  driver . FindElement ( By . TagName ( "button" ));   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      text_box  =  driver . find_element ( name :  'my-text' ) 
     submit_button  =  driver . find_element ( tag_name :  'button' )   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        let  textBox  =  await  driver . findElement ( By . name ( 'my-text' )); 
       let  submitButton  =  await  driver . findElement ( By . css ( 'button' ));   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          var  textBox  =  driver . findElement ( By . name ( "my-text" )) 
         val  submitButton  =  driver . findElement ( By . cssSelector ( "button" ))   
  
     
 
6. 要素に対してアクションを実行する 
There are only a handful of actions to take on an element ,
but you will use them frequently.
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          textBox . sendKeys ( "Selenium" ); 
         submitButton . click ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      text_box . send_keys ( "Selenium" ) 
     submit_button . click ()   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              textBox . SendKeys ( "Selenium" ); 
             submitButton . Click ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      text_box . send_keys ( 'Selenium' ) 
     submit_button . click   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        await  textBox . sendKeys ( 'Selenium' ); 
       await  submitButton . click ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          textBox . sendKeys ( "Selenium" ) 
         submitButton . click ()   
  
     
 
7. 要素に関する情報をリクエストします 
Elements store a lot of information that can be requested .
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          String  value  =  message . getText ();   
  
     
    
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
              var  value  =  message . Text ;   
  
     
    
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
        let  value  =  await  message . getText ();   
  
     
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
          driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )   
  
     
 
8. セッションを終了します 
This ends the driver process, which by default closes the browser as well.
No more commands can be sent to this driver instance.
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
    
    
    
    
        
  
  
  
  
  
  
  
  
    
    
    
    
    
    
  
      after ( async  ()  =>  await  driver . quit ());   
  
     
    
 
Putting everything together 
これらの8つを組み合わせて、使う必要のあるライブラリを含む完全なスクリプトにしましょう。
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
  
  
  
  
  
  
  
  
  package  dev.selenium.getting_started ; 
 import  org.junit.jupiter.api.Test ; 
import  org.openqa.selenium.By ; 
import  org.openqa.selenium.WebDriver ; 
import  org.openqa.selenium.WebElement ; 
import  org.openqa.selenium.chrome.ChromeDriver ; 
 import  java.time.Duration ; 
 import static  org.junit.jupiter.api.Assertions.assertEquals ; 
 public  class  FirstScriptTest  { 
     @Test 
     public  void  eightComponents ()  { 
         WebDriver  driver  =  new  ChromeDriver (); 
         driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" ); 
 
         String  title  =  driver . getTitle (); 
         assertEquals ( "Web form" ,  title ); 
 
         driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 )); 
 
         WebElement  textBox  =  driver . findElement ( By . name ( "my-text" )); 
         WebElement  submitButton  =  driver . findElement ( By . cssSelector ( "button" )); 
 
         textBox . sendKeys ( "Selenium" ); 
         submitButton . click (); 
 
         WebElement  message  =  driver . findElement ( By . id ( "message" )); 
         String  value  =  message . getText (); 
         assertEquals ( "Received!" ,  value ); 
 
         driver . quit (); 
     } 
 
 } 
 
  
     
    
        
  
  
  
  
  
  
  
  
  from  selenium  import  webdriver 
from  selenium.webdriver.common.by  import  By 
 
 def  test_eight_components (): 
    driver  =  webdriver . Chrome () 
 
     driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" ) 
 
     title  =  driver . title 
     assert  title  ==  "Web form" 
 
     driver . implicitly_wait ( 0.5 ) 
 
     text_box  =  driver . find_element ( by = By . NAME ,  value = "my-text" ) 
     submit_button  =  driver . find_element ( by = By . CSS_SELECTOR ,  value = "button" ) 
 
     text_box . send_keys ( "Selenium" ) 
     submit_button . click () 
 
     message  =  driver . find_element ( by = By . ID ,  value = "message" ) 
     value  =  message . text 
     assert  value  ==  "Received!" 
 
     driver . quit () 
  
  
     
    
        
  
  
  
  
  
  
  
  
  using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Chrome ; 
 namespace  SeleniumDocs.GettingStarted 
{ 
    [TestClass] 
    public  class  FirstScriptTest 
     { 
 
         [TestMethod] 
        public  void  ChromeSession () 
         { 
             IWebDriver  driver  =  new  ChromeDriver (); 
 
             driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" ); 
 
             var  title  =  driver . Title ; 
             Assert . AreEqual ( "Web form" ,  title ); 
 
             driver . Manage (). Timeouts (). ImplicitWait  =  TimeSpan . FromMilliseconds ( 500 ); 
 
             var  textBox  =  driver . FindElement ( By . Name ( "my-text" )); 
             var  submitButton  =  driver . FindElement ( By . TagName ( "button" )); 
             
             textBox . SendKeys ( "Selenium" ); 
             submitButton . Click (); 
             
             var  message  =  driver . FindElement ( By . Id ( "message" )); 
             var  value  =  message . Text ; 
             Assert . AreEqual ( "Received!" ,  value ); 
             
             driver . Quit (); 
         } 
     } 
 }  
  
     
    
        
  
  
  
  
  
  
  
  
  # frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'First Script'  do 
  it  'uses eight components'  do 
     driver  =  Selenium :: WebDriver . for  :chrome 
 
     driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' ) 
 
     title  =  driver . title 
     expect ( title ) . to  eq ( 'Web form' ) 
 
     driver . manage . timeouts . implicit_wait  =  500 
 
     text_box  =  driver . find_element ( name :  'my-text' ) 
     submit_button  =  driver . find_element ( tag_name :  'button' ) 
 
     text_box . send_keys ( 'Selenium' ) 
     submit_button . click 
 
     message  =  driver . find_element ( id :  'message' ) 
     value  =  message . text 
     expect ( value ) . to  eq ( 'Received!' ) 
 
     driver . quit 
   end 
 end 
 
  
     
    
        
  
  
  
  
  
  
  
  
  const  { By ,  Builder ,  Browser }  =  require ( 'selenium-webdriver' ); 
const  { suite }  =  require ( 'selenium-webdriver/testing' ); 
const  assert  =  require ( "assert" ); 
 suite ( function  ( env )  { 
  describe ( 'First script' ,  function  ()  { 
     let  driver ; 
 
     before ( async  function  ()  { 
       driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
     }); 
 
     after ( async  ()  =>  await  driver . quit ()); 
 
     it ( 'First Selenium script' ,  async  function  ()  { 
       await  driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' ); 
 
       let  title  =  await  driver . getTitle (); 
       assert . equal ( "Web form" ,  title ); 
 
       await  driver . manage (). setTimeouts ({ implicit :  500 }); 
 
       let  textBox  =  await  driver . findElement ( By . name ( 'my-text' )); 
       let  submitButton  =  await  driver . findElement ( By . css ( 'button' )); 
 
       await  textBox . sendKeys ( 'Selenium' ); 
       await  submitButton . click (); 
 
       let  message  =  await  driver . findElement ( By . id ( 'message' )); 
       let  value  =  await  message . getText (); 
       assert . equal ( "Received!" ,  value ); 
     }); 
   }); 
 },  {  browsers :  [ Browser . CHROME ,  Browser . FIREFOX ]});  
  
     
    
        
  
  
  
  
  
  
  
  
  package  dev.selenium.getting_started 
 import  org.junit.jupiter.api.* 
import  org.junit.jupiter.api.Assertions.assertEquals 
import  org.openqa.selenium.By 
import  org.openqa.selenium.WebDriver 
import  org.openqa.selenium.chrome.ChromeDriver 
import  java.time.Duration 
 @TestInstance ( TestInstance . Lifecycle . PER_CLASS ) 
class  FirstScriptTest  { 
    private  lateinit  var  driver :  WebDriver 
 
     @Test 
     fun  eightComponents ()  { 
         driver  =  ChromeDriver () 
 
         driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" ) 
 
         val  title  =  driver . title 
         assertEquals ( "Web form" ,  title ) 
 
         driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 )) 
 
         var  textBox  =  driver . findElement ( By . name ( "my-text" )) 
         val  submitButton  =  driver . findElement ( By . cssSelector ( "button" )) 
 
         textBox . sendKeys ( "Selenium" ) 
         submitButton . click () 
 
         val  message  =  driver . findElement ( By . id ( "message" )) 
         val  value  =  message . getText () 
         assertEquals ( "Received!" ,  value ) 
 
         driver . quit () 
     } 
 
 }  
  
     
 
Test Runners 
If you are using Selenium for testing,
you will want to execute your Selenium code using test runner tools.
Many of the code examples in this documentation can be found in our example repositories.
There are multiple options in each language, but here is what we are using in our examples:
  
      
        Java
       
     
      
        Python
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        JavaScript
       
     
      
        Kotlin
       
     
 
    
        
// Add instructions
    
    
        
// Add instructions
    
    
        
// Add instructions
    
    
        
// Add instructions
    
    
    
        Install Mocha Test runner using below command in your terminal
Install with npm globally:
or as a development dependency for your project:
npm install --save-dev mocha
 and run your tests using below command
mocha firstScript.spec.js
  
     
    
        
// Add instructions
    
 
Next Steps 
Take what you’ve learned and build out your Selenium code.
As you find more functionality that you need, read up on the rest of our
WebDriver documentation .
 
    
	
  
    
    
	
    
    
	3 - Selenium4にアップグレードする方法 
    Selenium 4に興味がありますか? 最新リリースへのアップグレードに役立つこのガイドを確認してください。
	公式にサポートされている言語(Ruby、JavaScript、C#、Python、およびJava)のいずれかを使用している場合、
Selenium4へのアップグレードは簡単なプロセスです。
いくつかの問題が発生する可能性がある場合があるかもしれません。このガイドは、それらを整理するのに役立ちます。
プロジェクトの依存関係をアップグレードする手順を実行し、バージョンのアップグレードによってもたらされる主な非推奨と変更を理解します。
これが、Selenium4にアップグレードするために実行する手順です。
テストコードの準備 
依存関係のアップグレード 
潜在的なエラーと非推奨メッセージ 
 
注:Selenium 3.xバージョンの開発中に、W3CWebDriver標準のサポートが実装されました。
この新しいプロトコルと従来のJSONワイヤープロトコルの両方がサポートされました。
バージョン3.11の前後で、SeleniumコードはレベルW3C1仕様に準拠するようになりました。
Selenium 3の最新バージョンのW3C準拠のコードは、Selenium4で期待どおりに機能します。
テストコードの準備 
Selenium 4は、レガシープロトコルのサポートを削除し、内部でデフォルトでW3CWebDriver標準を使用します。
ほとんどの場合、この実装はエンドユーザーに影響を与えません。
主な例外は、Capabilitiesと アクション クラスです。
Capabilities 
テスト機能がW3Cに準拠するように構成されていない場合、セッションが開始されない可能性があります。
W3CWebDriverの標準機能のリストは次のとおりです。
browserName 
browserVersion (version に変更) 
platformName (platform に変更) 
acceptInsecureCerts 
pageLoadStrategy 
proxy 
timeouts 
unhandledPromptBehavior 
 
標準Capabilitiesの最新リストは、 W3C WebDriver  にあります。
上記のリストに含まれていないCapabilitiesには、ベンダープレフィックスを含める必要があります。
これは、ブラウザ固有のCapabilitiesとクラウドベンダー固有のCapabilitiesに適用されます。
たとえば、クラウドベンダーがテストに build Capabilities と name Capabilitiesを使用している場合は、
それらを cloud:options ブロックでラップする必要があります(適切なプレフィックスについては、クラウドベンダーに確認してください)。
Before 
  
      
        Java
       
     
      
        JavaScript
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        Python
       
     
 
    
        DesiredCapabilities  caps  =  DesiredCapabilities . firefox (); 
caps . setCapability ( "platform" ,  "Windows 10" ); 
caps . setCapability ( "version" ,  "92" ); 
caps . setCapability ( "build" ,  myTestBuild ); 
caps . setCapability ( "name" ,  myTestName ); 
WebDriver  driver  =  new  RemoteWebDriver ( new  URL ( cloudUrl ),  caps );  
     
    
        caps  =  {}; 
caps [ 'browserName' ]  =  'Firefox' ; 
caps [ 'platform' ]  =  'Windows 10' ; 
caps [ 'version' ]  =  '92' ; 
caps [ 'build' ]  =  myTestBuild ; 
caps [ 'name' ]  =  myTestName ;  
     
    
        DesiredCapabilities  caps  =  new  DesiredCapabilities (); 
caps . SetCapability ( "browserName" ,  "firefox" ); 
caps . SetCapability ( "platform" ,  "Windows 10" ); 
caps . SetCapability ( "version" ,  "92" ); 
caps . SetCapability ( "build" ,  myTestBuild ); 
caps . SetCapability ( "name" ,  myTestName ); 
var  driver  =  new  RemoteWebDriver ( new  Uri ( CloudURL ),  caps );  
     
    
        caps  =  Selenium :: WebDriver :: Remote :: Capabilities . firefox 
caps [ :platform ]  =  'Windows 10' 
caps [ :version ]  =  '92' 
caps [ :build ]  =  my_test_build 
caps [ :name ]  =  my_test_name 
driver  =  Selenium :: WebDriver . for  :remote ,  url :  cloud_url ,  desired_capabilities :  caps  
     
    
        caps  =  {} 
caps [ 'browserName' ]  =  'firefox' 
caps [ 'platform' ]  =  'Windows 10' 
caps [ 'version' ]  =  '92' 
caps [ 'build' ]  =  my_test_build 
caps [ 'name' ]  =  my_test_name 
driver  =  webdriver . Remote ( cloud_url ,  desired_capabilities = caps )  
     
 
After 
  
      
        Java
       
     
      
        JavaScript
       
     
      
        CSharp
       
     
      
        Ruby
       
     
      
        Python
       
     
 
    
        FirefoxOptions  browserOptions  =  new  FirefoxOptions (); 
browserOptions . setPlatformName ( "Windows 10" ); 
browserOptions . setBrowserVersion ( "92" ); 
Map < String ,  Object >  cloudOptions  =  new  HashMap <>(); 
cloudOptions . put ( "build" ,  myTestBuild ); 
cloudOptions . put ( "name" ,  myTestName ); 
browserOptions . setCapability ( "cloud:options" ,  cloudOptions ); 
WebDriver  driver  =  new  RemoteWebDriver ( new  URL ( cloudUrl ),  browserOptions );  
     
    
        capabilities  =  { 
  browserName :  'firefox' , 
   browserVersion :  '92' , 
   platformName :  'Windows 10' , 
   'cloud:options' :  { 
      build :  myTestBuild , 
      name :  myTestName , 
   } 
 }  
     
    
        var  browserOptions  =  new  FirefoxOptions (); 
browserOptions . PlatformName  =  "Windows 10" ; 
browserOptions . BrowserVersion  =  "92" ; 
var  cloudOptions  =  new  Dictionary < string ,  object >(); 
cloudOptions . Add ( "build" ,  myTestBuild ); 
cloudOptions . Add ( "name" ,  myTestName ); 
browserOptions . AddAdditionalOption ( "cloud:options" ,  cloudOptions ); 
var  driver  =  new  RemoteWebDriver ( new  Uri ( CloudURL ),  browserOptions );  
     
    
        options  =  Selenium :: WebDriver :: Options . firefox 
options . browser_version  =  'latest' 
options . platform_name  =  'Windows 10' 
cloud_options  =  {} 
cloud_options [ :build ]  =  my_test_build 
cloud_options [ :name ]  =  my_test_name 
options . add_option ( 'cloud:options' ,  cloud_options ) 
driver  =  Selenium :: WebDriver . for  :remote ,  url :  cloud_url ,  capabilities :  options  
     
    
        from  selenium.webdriver.firefox.options  import  Options  as  FirefoxOptions 
options  =  FirefoxOptions () 
options . browser_version  =  '92' 
options . platform_name  =  'Windows 10' 
cloud_options  =  {} 
cloud_options [ 'build' ]  =  my_test_build 
cloud_options [ 'name' ]  =  my_test_name 
options . set_capability ( 'cloud:options' ,  cloud_options ) 
driver  =  webdriver . Remote ( cloud_url ,  options = options )  
     
 
Javaで要素ユーティリティメソッドを検索する 
Javaバインディング(FindsBy インターフェイス)の要素を検索するユーティリティメソッドは、内部使用のみを目的としていたため、削除されました。
次のコードサンプルは、これを分かりやすく説明しています。
findElement * で単一の要素を検索する。
    
    
 driver . findElementByClassName ( "className" ); 
driver . findElementByCssSelector ( ".className" ); 
driver . findElementById ( "elementId" ); 
driver . findElementByLinkText ( "linkText" ); 
driver . findElementByName ( "elementName" ); 
driver . findElementByPartialLinkText ( "partialText" ); 
driver . findElementByTagName ( "elementTagName" ); 
driver . findElementByXPath ( "xPath" ); 
 
   
    
    
 driver . findElement ( By . className ( "className" )); 
driver . findElement ( By . cssSelector ( ".className" )); 
driver . findElement ( By . id ( "elementId" )); 
driver . findElement ( By . linkText ( "linkText" )); 
driver . findElement ( By . name ( "elementName" )); 
driver . findElement ( By . partialLinkText ( "partialText" )); 
driver . findElement ( By . tagName ( "elementTagName" )); 
driver . findElement ( By . xpath ( "xPath" )); 
 
   
 
findElements * で複数の要素を検索する。
    
    
 driver . findElementsByClassName ( "className" ); 
driver . findElementsByCssSelector ( ".className" ); 
driver . findElementsById ( "elementId" ); 
driver . findElementsByLinkText ( "linkText" ); 
driver . findElementsByName ( "elementName" ); 
driver . findElementsByPartialLinkText ( "partialText" ); 
driver . findElementsByTagName ( "elementTagName" ); 
driver . findElementsByXPath ( "xPath" ); 
 
   
    
    
 driver . findElements ( By . className ( "className" )); 
driver . findElements ( By . cssSelector ( ".className" )); 
driver . findElements ( By . id ( "elementId" )); 
driver . findElements ( By . linkText ( "linkText" )); 
driver . findElements ( By . name ( "elementName" )); 
driver . findElements ( By . partialLinkText ( "partialText" )); 
driver . findElements ( By . tagName ( "elementTagName" )); 
driver . findElements ( By . xpath ( "xPath" )); 
 
   
 
依存関係のアップグレード 
以下のサブセクションを確認してSelenium4をインストールし、プロジェクトの依存関係をアップグレードしてください。
Java 
Seleniumをアップグレードするプロセスは、使用されているビルドツールによって異なります。
Javaで最も一般的なものであるMaven とGradle について説明します。
必要なJavaの最小バージョンはまだ8です。
Maven 
    
    
 <dependencies> 
  <!-- more dependencies ... --> 
   <dependency> 
     <groupId> org.seleniumhq.selenium</groupId> 
     <artifactId> selenium-java</artifactId> 
     <version> 3.141.59</version> 
   </dependency> 
   <!-- more dependencies ... --> 
 </dependencies> 
 
   
    
    
 <dependencies> 
    <!-- more dependencies ... --> 
     <dependency> 
         <groupId> org.seleniumhq.selenium</groupId> 
         <artifactId> selenium-java</artifactId> 
         <version> 4.4.0</version> 
     </dependency> 
     <!-- more dependencies ... --> 
 </dependencies> 
 
   
 
変更を加えた後、pom.xml ファイルと同じディレクトリで mvn clean compile を実行できます。
Gradle 
    
    
plugins {
    id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
    mavenCentral()
}
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
}
test {
    useJUnitPlatform()
}
 
   
    
    
plugins {
    id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
    mavenCentral()
}
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.4.0'
}
test {
    useJUnitPlatform()
}
 
   
 
変更を加えた後、 build.gradle ファイルと同じディレクトリで ./gradlew cleanbuild を実行できます。
すべてのJavaリリースを確認するには、 MVNRepository  にアクセスしてください。
C# 
C#でSelenium4の更新を取得する場所は NuGet  です。
Selenium.WebDriver  パッケージの下で、最新バージョンに更新するための手順を入手できます。
Visual Studio内では、NuGetパッケージマネージャーを使用して次の操作を実行できます。
PM> Install-Package Selenium.WebDriver -Version 4.4.0
 Python 
Pythonを使用するための最も重要な変更は、最低限必要なバージョンです。
Selenium 4には、Python3.7以降が必要です。
詳細については、Python Package Index を参照してください。
コマンドラインからアップグレードするには、次のコマンドを実行できます。
pip install selenium == 4.4.3
 Ruby 
Selenium 4の更新の詳細は、RubyGemsのselenium-webdriver で確認できます。
最新バージョンをインストールするには、次のコマンドを実行できます。
gem install selenium-webdriver
 Gemfileには下記のように追加します。
gem 'selenium-webdriver' , '~> 4.4.0' 
 JavaScript 
selenium-webdriverパッケージは、Nodeパッケージマネージャーのnpmjs にあります。
Selenium4はhere にあります。
これをインストールするには、次のいずれかを実行します。
npm install selenium-webdriver
 または、package.jsonを更新して、 npm install を実行します。
{ 
  "name" :  "selenium-tests" , 
   "version" :  "1.0.0" , 
   "dependencies" :  { 
     "selenium-webdriver" :  "^4.4.0" 
   } 
 } 
潜在的なエラーと非推奨メッセージ 
これは、Selenium4にアップグレードした後に発生する可能性のある非推奨メッセージを克服するのに役立つ一連のコード例です。
Java 
待機とタイムアウト 
タイムアウトで受信するパラメーターは、期待値 (long time, TimeUnit unit) から期待値 (Duration duration) に替わりました。
    
    
 driver . manage (). timeouts (). implicitlyWait ( 10 ,  TimeUnit . SECONDS ); 
driver . manage (). timeouts (). setScriptTimeout ( 2 ,  TimeUnit . MINUTES ); 
driver . manage (). timeouts (). pageLoadTimeout ( 10 ,  TimeUnit . SECONDS ); 
 
   
    
    
 driver . manage (). timeouts (). implicitlyWait ( Duration . ofSeconds ( 10 )); 
driver . manage (). timeouts (). scriptTimeout ( Duration . ofMinutes ( 2 )); 
driver . manage (). timeouts (). pageLoadTimeout ( Duration . ofSeconds ( 10 )); 
 
   
 
現在、待機も異なるパラメーターを期待しています。
WebDriverWaitは、秒とミリ秒単位のタイムアウトに、 long ではなくDurationを期待するようになりました。
FluentWait の withTimeout および pollingEvery ユーティリティメソッドは、期待値 (long time, TimeUnit unit) から (Duration duration) に替わりました。
    
    
 new  WebDriverWait ( driver ,  3 ) 
. until ( ExpectedConditions . elementToBeClickable ( By . cssSelector ( "#id" ))); 
 Wait < WebDriver >  wait  =  new  FluentWait < WebDriver >( driver ) 
  . withTimeout ( 30 ,  TimeUnit . SECONDS ) 
   . pollingEvery ( 5 ,  TimeUnit . SECONDS ) 
   . ignoring ( NoSuchElementException . class ); 
  
   
    
    
 new  WebDriverWait ( driver ,  Duration . ofSeconds ( 3 )) 
  . until ( ExpectedConditions . elementToBeClickable ( By . cssSelector ( "#id" ))); 
 
   Wait < WebDriver >  wait  =  new  FluentWait < WebDriver >( driver ) 
   . withTimeout ( Duration . ofSeconds ( 30 )) 
   . pollingEvery ( Duration . ofSeconds ( 5 )) 
   . ignoring ( NoSuchElementException . class ); 
  
   
 
マージCapabilitiesは、もはや呼び出し元のオブジェクトを変更しなくなりました 
以前は、別のCapabilitiesセットを別のセットにマージすることが可能であり、呼び出し元のオブジェクトを変更していました。
今は、ここで、マージ操作の結果を割り当てる必要があります。
    
    
 MutableCapabilities  capabilities  =  new  MutableCapabilities (); 
capabilities . setCapability ( "platformVersion" ,  "Windows 10" ); 
FirefoxOptions  options  =  new  FirefoxOptions (); 
options . setHeadless ( true ); 
options . merge ( capabilities ); 
As  a  result ,  the  ` options `  object  was  getting  modified . 
 
   
    
    
 MutableCapabilities  capabilities  =  new  MutableCapabilities (); 
capabilities . setCapability ( "platformVersion" ,  "Windows 10" ); 
FirefoxOptions  options  =  new  FirefoxOptions (); 
options . setHeadless ( true ); 
options  =  options . merge ( capabilities ); 
The  result  of  the  ` merge `  call  needs  to  be  assigned  to  an  object . 
 
   
 
古いFirefox 
GeckoDriverが登場する前は、SeleniumプロジェクトにはFirefoxを自動化するためのドライバー実装がありました(バージョン<48)。
ただし、この実装は最近のバージョンのFirefoxでは機能しないため、もう必要ありません。
Selenium 4にアップグレードする際の大きな問題を回避するために、setLegacy オプションは非推奨として表示されます。
古い実装の使用をやめ、GeckoDriverのみに依存することをお勧めします。
次のコードは、アップグレード後に非推奨になったsetLegacy 行を示しています。
FirefoxOptions  options  =  new  FirefoxOptions (); 
options . setLegacy ( true ); 
BrowserType 
BrowserType インターフェースは長い間使用されてきましたが、新しい Browser インターフェースを優先して非推奨になります。
    
    
 MutableCapabilities  capabilities  =  new  MutableCapabilities (); 
capabilities . setCapability ( "browserVersion" ,  "92" ); 
capabilities . setCapability ( "browserName" ,  BrowserType . FIREFOX ); 
 
   
    
    
 MutableCapabilities  capabilities  =  new  MutableCapabilities (); 
capabilities . setCapability ( "browserVersion" ,  "92" ); 
capabilities . setCapability ( "browserName" ,  Browser . FIREFOX ); 
 
   
 
C# 
AddAdditionalCapability は非推奨になりました 
その代わりに、 AddAdditionalOption をお勧めします。 これを示す例を次に示します。
    
    
var browserOptions = new ChromeOptions();
browserOptions.PlatformName = "Windows 10";
browserOptions.BrowserVersion = "latest";
var cloudOptions = new Dictionary<string, object>();
browserOptions.AddAdditionalCapability("cloud:options", cloudOptions, true);
 
   
    
    
var browserOptions = new ChromeOptions();
browserOptions.PlatformName = "Windows 10";
browserOptions.BrowserVersion = "latest";
var cloudOptions = new Dictionary<string, object>();
browserOptions.AddAdditionalOption("cloud:options", cloudOptions);
 
   
 
Python 
execute_pathは非推奨になりました。Serviceオブジェクトを渡してください 
Selenium 4では、非推奨の警告を防ぐために、Serviceオブジェクトからドライバーの executable_path を設定する必要があります。
(または、PATHを設定せず、代わりに必要なドライバーがシステムPATH上にあることを確認してください。)
    
    
 from  selenium  import  webdriver 
options  =  webdriver . ChromeOptions () 
driver  =  webdriver . Chrome ( 
    executable_path = CHROMEDRIVER_PATH ,  
     options = options 
 ) 
 
   
    
    
 from  selenium  import  webdriver 
from  selenium.webdriver.chrome.service  import  Service  as  ChromeService 
options  =  webdriver . ChromeOptions () 
service  =  ChromeService ( executable_path = CHROMEDRIVER_PATH ) 
driver  =  webdriver . Chrome ( service = service ,  options = options ) 
 
   
 
まとめ 
Selenium 4にアップグレードする際に考慮すべき主な変更点を確認しました。
アップグレードのためにテストコードを準備する際にカバーするさまざまな側面について説明します。
これには、新しいバージョンのSeleniumを使用する時に発生する可能性のある潜在的な問題を防ぐ方法の提案も含まれます。
最後に、アップグレード後に発生する可能性のある一連の問題についても説明し、それらの問題に対する潜在的な修正を共有しました。
これは元々は https://saucelabs.com/resources/articles/how-to-upgrade-to-selenium-4  に投稿されました