セレンのChromeドライバーでテストを実行中

Google Chromeのブラウザは、ChromeDriver.exeという実行ファイルを使用してWebDriverプロトコルを実装しています。この実行ファイルは、システム上でサーバーを起動し、テストはすべてこのサーバーと通信してテストを実行します。この記事では、Selenium ChromeDriverの最新バージョンのダウンロード方法について学びます。

  • How to setup Selenium ChromeDriver in multiple ways

SeleniumのChromeDriverをダウンロードしてください。

まず、最新版のChromeDriverをダウンロードする必要があります。これは、最新のChromeバージョンに対応しており、すべてのバグ修正が含まれているためです。ChromeDriverをダウンロードする手順は以下の通りです。
– ステップ1:クロミウム公式ウェブサイトにアクセスし、お使いのオペレーティングシステムに基づいた最新版のChromeDriverをダウンロードします。

Chrome Version
  • Step 2: Click on ChromeDriver 73.0.3683.20 link. You will be navigated to ChromeDriver download page which contains ChromeDriver for Linux, Mac and Windows operating systems.
    Note: Here we are working on Windows Operating system, we need to download the corresponding Chrome driver of Windows version. If your Operating System is Linux or Mac then you need to download the corresponding Chrome driver.
Chrome Index
  • Step 3: Click on chromedriver_win32.zip to download ChromeDriver for Windows.
  • Step 4: Once the zip file is downloaded, you can unzip it to retrieve chromedriver.exe. Note the location where you extracted the ChromeDriver. Location will be later used to instantiate the driver.
Chrome Driver

Selenium WebDriverを使用してChromeブラウザを起動する。

他のドライバと同様に、Chromeドライバを起動することは簡単です。 WebDriver = new ChromeDriver();

package com.scdev.selenium.Chrome;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDriver {

	public static void main(String[] args) {
		WebDriver driver= new ChromeDriver();
		driver.get("https://scdev.com");
     }
} 

上記のプログラムを実行すると、java.lang.IllegalStateExceptionという例外が発生します。それは、webdriver.chrome.driverによってドライバー実行可能ファイルへのパスが設定されている必要があることを伝えています。上記の問題を解決するために、seleniumコマンドをChromeで実行するためにChromeDriverをダウンロードする必要があります。すべてのブラウザにはドライバーがあります。Chrome用のドライバーはChromeDriverです。seleniumコマンドはChromeDriverによって解釈され、Chrome上で実行されます。

ChromeDriverを初期化するためのさまざまな方法

ChromeDriverを初期化するためには2つの方法があります- Webdriver.Chrome.Driverを使用してください。

  • Use Environment Variables

方法1: Webdriver.chrome.driver システムプロパティを使用してください。

次のネイティブな日本語でのパラフレーズを次に示します:
システムプロパティを設定するためのコードはあります。

System.setProperty(“webdriver.chrome.driver”,“Path to chromedriver.exe”);

ChromeDriverを起動するための完全なプログラムは次のようになります。

package com.scdev.selenium.Chrome;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDriver {

	public static void main(String[] args) {
                System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
		WebDriver driver= new ChromeDriver();
		driver.get("https://scdev.com");
                String PageTitle = driver.getTitle();
                System.out.println("Page Title is:" + PageTitle);
                driver.close();
     }
} 

上記のプログラムを実行すると、新しいChromeウィンドウでJournaldev.comが開かれ、ウェブサイトのタイトルがコンソールに表示されることに気付くでしょう。

方法2:Windows環境変数でChromeDriverのパスを設定する

  • Step 1: Go to My Computer and Right click to get the context menu.
MyComputer Properties
  • Step 2: Click on the Change Setting on the opened window.
Change Settings
  • Step 3: Click on Advance tab and click on Environment Variables.
System Properties
  • Step 4: Select Path under System Variables and click on Edit.
Environment Variables
  • Step 5: At the end of the string use semicolon and paste the path of the ChromeDriver. On my machine my ChromeDriver exe resides in D:\Drivers\
Path

注意:パスが設定されたら、スクリプトごとに毎回システムプロパティを設定する必要はありません。システムプロパティのコードなしでも、スクリプトは正常に動作します。ChromeDriverを起動するための完全なプログラムは、以下のようになります。


package com.scdev.selenium.Chrome;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromefoxDriver;

public class ChromeDriver {

    public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    driver.get("https://scdev.com");
    String PageTitle = driver.getTitle();
    System.out.println("Page Title is:" + PageTitle);
    driver.close();
    }
 }

上記のプログラムを実行すると、システムプロパティのコードなしでスクリプトが動作し、新しいChromeウィンドウでJournaldev.comが開かれ、ウェブサイトのタイトルがコンソールに表示されることに気付くでしょう。

コメントを残す 0

Your email address will not be published. Required fields are marked *