セレニウムのfindElementとfindElementsの例

ウェブページとやり取りする際に、ユーザーにはウェブ要素を見つけるように要求します。通常、WebDriverを使用してウェブアプリケーションを自動化する際には、ページ上のHTML要素を見つけることから始めます。Selenium WebDriverは、要素を特定するための2つのメソッド、findElementとfindElementsを定義しています。

    1. findElement(要素の検索): このコマンドはウェブページ内の要素を一意に特定するために使用されます。

 

    findElements(要素リストの検索): このコマンドはウェブページ内の要素のリストを一意に特定するために使用されます。

ウェブページ内のウェブ要素を一意に識別するためには、ID、名前、クラス名、リンクテキスト、部分リンクテキスト、タグ名、XPathのような複数の方法があります。

findElementとfindElementsメソッドの違い

FindElement()メソッド:

  • This command is used to access any single element on the web page
  • It will return the object of the first matching element of the specified locator
  • It will throw NoSuchElementException when it fails to identify the element

FindElements()メソッド:

  • This command is used to uniquely identify the list of web elements within the web page.
  • The usage of this method is very limited
  • If the element doesn’t exist on the page then, then it will return value with an empty list

セレニウムのfindElementコマンド

Find ElementコマンドはByオブジェクトをパラメータとして受け取り、WebElement型のオブジェクトを返します。Byオブジェクトは、ID、名前、クラス名、リンクテキスト、XPathなどのさまざまなロケータ戦略と組み合わせて使用できます。

FindElementコマンドの構文

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

Locatorの戦略は、以下の値のいずれかになり得ます。

  • ID
  • Name
  • Class Name
  • Tag Name
  • Link Text
  • Partial Link Text
  • XPath

ロケーター値は、ウェブ要素を識別するために使用する一意の値です。開発者とテスターの主な責任は、IDや名前などの特定のプロパティを使用してウェブ要素が一意に識別されるようにすることです。例:

WebElement login= driver.findElement(By.linkText("Login"));

セレンのfindElementsコマンド

セレンのfindElementsコマンドは、Byオブジェクトをパラメータとして受け取り、ウェブ要素のリストを返します。指定されたロケータ戦略とロケータ値を使用して、要素が見つからない場合は空のリストを返します。

FindElementsコマンドの構文

List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));

例:
英語をネイティブにするためには、日本語を習得することが重要です。

List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));

SeleniumのfindElementコマンドの使い方はどうですか。

以下のアプリケーションはデモの目的で使用されます。https://www.irctc.co.in/nget/user-registration シナリオ

    1. https://www.irctc.co.in/nget/user-registration を開いてください。

 

    ラジオボタンを見つけてクリックしてください。
package com.scdev.selenium.findelement;

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

public class SeleniumFindElement { 
       public static void main (String [] args){ 

     System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
     WebDriver driver= new ChromeDriver();
     driver.manage().window.maximize():
     driver.get(:"https://www.irctc.co.in/nget/user-registration");

//Find the radio button for "Male" by using ID and click on it
driver.findElement(By.id("M")).click();

       } 
}

SeleniumのfindElementsコマンドの使い方はどうすればいいですか?

以下のアプリケーションはデモの目的で使用されます。https://www.irctc.co.in/nget/user-registrationのシナリオです。

    1. AUTのためにhttps://www.irctc.co.in/nget/user-registrationを開く

 

    ラジオボタンのテキストを見つけてコンソールに出力する

package com.scdev.selenium.findelements;

import java.util.List;

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

public class SeleniumFindElements {

	public static void main(String[] args) {

	System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
	WebDriver driver= new ChromeDriver();
        driver.get("https://www.irctc.co.in/nget/user-registration");
        List<WebElement> elements = driver.findElements(By.id("M"));
        System.out.println("Number of elements:" +elements.size());

       for(int i=0; i<elements.size(); i++){
       System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
	}
     }
}

セレニウムのロケータにアクセスするための複数の戦略で探索してください。

セレニウムウェブドライバーは、findElement(By.)メソッドを使用してウェブ要素に参照します。findElementメソッドは、<“By”>と呼ばれるロケーターオブジェクトを使用します。要件に応じて使用できるさまざまな種類の「By」戦略があります。

1. IDで。

コマンド:driver.findElement(By.id(<要素のID>)) 例: idで入力要素を見つけるためのJavaの例コード

WebElement user = driver.findElement(By.id("JournalDev"));

2. 名前によって

コマンド:driver.findElement(By.name(<要素名>)) 例: 名前で入力要素を検索するためのJavaの例題コード

WebElement user = driver.findElement(By.name("JournalDev"));

3. クラス名によって

コマンド:driver.findElement(By.className())
例:<input class=“JournalDev”>
要素のクラス名で入力要素を検索するためのJavaのコードの例。

WebElement user = driver.findElement(By.className("JournalDev"));

4. LinkTextによって

コマンド:driver.findElement(By.linkText(<リンクテキスト>))

例: JournalDev-1 JournalDev-2

リンクテキストまたは部分的なリンクテキストに一致する要素を見つけるためのJavaのサンプルコード:

WebElement link = driver.findElement(By.linkText("JournalDev-1"));
WebElement link = driver.findElement(By.partialLinkText("JournalDev-2"));

5. CSSセレクタによって

コマンド: driver.findElement(By.cssSelector(<cssセレクタ>))
例: リンクまたは部分的なリンクテキストに一致する要素を見つけるためのJavaの例題コード:

WebElement emailText = driver.findElement(By.cssSelector("input#email"));

6. XPathで

コマンド: driver.findElement(By.xpath()) XPathのJavaの例のコード:

// Absolute path
WebElement item = driver.findElement(By.xpath("html/head/body/table/tr/td"));

// Relative path
WebElement item = driver.findElement(By.xpath("//input"));

// Finding elements using indexes
WebElement item = driver.findElement(By.xpath("//input[2]"));
```****
コメントを残す 0

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