選択要素の操作

一部の要素では、自動化するためにかなりのボイラープレートコードが必要になる場合があります。 これを減らしてテストをきれいにするために、Seleniumサポートパッケージに Select クラスがあります。 それを使用するには、次のimportステートメントが必要です。

import org.openqa.selenium.support.ui.Select;
  
from selenium.webdriver.support.select import Select
  
using OpenQA.Selenium.Support.UI
  
include Selenium::WebDriver::Support
  
// This feature is not implemented - Help us by sending a pr to implement this feature 
  
import org.openqa.selenium.support.ui.Select
  

そして、 <select> 要素を参照するWebElementを使用してSelectオブジェクトを作成できます。

WebElement selectElement = driver.findElement(By.id("selectElementID"));
Select selectObject = new Select(selectElement);
  
select_element = driver.find_element(By.ID,'selectElementID')
select_object = Select(select_element)
  
IWebElement selectElement = driver.FindElement(By.Id("selectElementID"));
var selectObject = new SelectElement(selectElement);
  
select_element = driver.find_element(id: 'selectElementID')
select_object = Select(select_element)
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
val selectElement = driver.findElement(By.id("selectElementID"))
val selectObject = new Select(selectElement)
  

Selectオブジェクトは、 <select> 要素とやり取りできる一連のコマンドを提供します。 まず、 <select> 要素からオプションを選択するさまざまな方法があります。

<select>
 <option value=value1>Bread</option>
 <option value=value2 selected>Milk</option>
 <option value=value3>Cheese</option>
</select>

上記の要素から最初のオプションを選択するには、3つの方法があります。

// Select an <option> based upon the <select> element's internal index
selectObject.selectByIndex(1);

// Select an <option> based upon its value attribute
selectObject.selectByValue("value1");

// Select an <option> based upon its text
selectObject.selectByVisibleText("Bread");
  
# Select an <option> based upon the <select> element's internal index
select_object.select_by_index(1)

# Select an <option> based upon its value attribute
select_object.select_by_value('value1')

# Select an <option> based upon its text
select_object.select_by_visible_text('Bread')
  
// Select an <option> based upon the <select> element's internal index
selectObject.SelectByIndex(1);

// Select an <option> based upon its value attribute
selectObject.SelectByValue("value1");

// Select an <option> based upon its text
 selectObject.SelectByText("Bread");
  
# Select an <option> based upon the <select> element's internal index
select_object.select_by(:index, 1)

# Select an <option> based upon its value attribute
select_object.select_by(:value, 'value1')

# Select an <option> based upon its text
select_object.select_by(:text, 'Bread')
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
// Select an <option> based upon the <select> element's internal index
selectObject.selectByIndex(1)

// Select an <option> based upon its value attribute
selectObject.selectByValue("value1")

// Select an <option> based upon its text
selectObject.selectByVisibleText("Bread")
  

以下を使用して、選択されているオプションを確認できます。

// Return a List<WebElement> of options that have been selected
List<WebElement> allSelectedOptions = selectObject.getAllSelectedOptions();

// Return a WebElement referencing the first selection option found by walking down the DOM
WebElement firstSelectedOption = selectObject.getFirstSelectedOption();
  
# Return a list[WebElement] of options that have been selected
all_selected_options = select_object.all_selected_options

# Return a WebElement referencing the first selection option found by walking down the DOM
first_selected_option = select_object.first_selected_option
  
// Return a List<WebElement> of options that have been selected
var allSelectedOptions = selectObject.AllSelectedOptions;

// Return a WebElement referencing the first selection option found by walking down the DOM
var firstSelectedOption = selectObject.AllSelectedOptions.FirstOrDefault();
  
# Return an Array[Element] of options that have been selected
all_selected_options = select_object.selected_options

# Return a WebElement referencing the first selection option found by walking down the DOM
first_selected_option = select_object.first_selected_option
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
// Return a List<WebElement> of options that have been selected
val allSelectedOptions = selectObject.allSelectedOptions

// Return a WebElement referencing the first selection option found by walking down the DOM
val firstSelectedOption = selectObject.firstSelectedOption
  

または、 <select> 要素に含まれる <option> 要素に興味があるかもしれません。

// Return a List<WebElement> of options that the <select> element contains
List<WebElement> allAvailableOptions = selectObject.getOptions();
  
# Return a list[WebElement] of options that the &lt;select&gt; element contains
all_available_options = select_object.options
  
// Return a IList<IWebElement> of options that the <select> element contains
IList<IWebElement> allAvailableOptions = selectObject.Options;
  
# Return an Array[Element] of options that the &lt;select&gt; element contains
all_available_options = select_object.options
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
// Return a List<WebElement> of options that the <select> element contains
val allAvailableOptions = selectObject.options
  

要素の選択を解除する場合は、4つの選択肢があります。

// Deselect an <option> based upon the <select> element's internal index
selectObject.deselectByIndex(1);

// Deselect an <option> based upon its value attribute
selectObject.deselectByValue("value1");

// Deselect an <option> based upon its text
selectObject.deselectByVisibleText("Bread");

// Deselect all selected <option> elements
selectObject.deselectAll();
  
# Deselect an <option> based upon the <select> element's internal index
select_object.deselect_by_index(1)

# Deselect an <option> based upon its value attribute
select_object.deselect_by_value('value1')

# Deselect an <option> based upon its text
select_object.deselect_by_visible_text('Bread')

# Deselect all selected <option> elements
select_object.deselect_all()
  
// Deselect an <option> based upon the <select> element's internal index
selectObject.DeselectByIndex(1);

// Deselect an <option> based upon its value attribute
selectObject.DeselectByValue("value1");

// Deselect an <option> based upon its text
selectObject.DeselectByText("Bread");

// Deselect all selected <option> elements
selectObject.DeselectAll();
  
# Deselect an <option> based upon the <select> element's internal index
select_object.deselect_by(:index, 1)

# Deselect an <option> based upon its value attribute
select_object.deselect_by(:value, 'value1')

# Deselect an <option> based upon its text
select_object.deselect_by(:text, 'Bread')

# Deselect all selected <option> elements
select_object.deselect_all
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
// Deselect an <option> based upon the <select> element's internal index
selectObject.deselectByIndex(1)

// Deselect an <option> based upon its value attribute
selectObject.deselectByValue("value1")

// Deselect an <option> based upon its text
selectObject.deselectByVisibleText("Bread")

// Deselect all selected <option> elements
selectObject.deselectAll()
  

最後に、一部の <select> 要素を使用すると、複数のオプションを選択できます。 <select> 要素がこれらの1つであるかどうかを調べるには、下記のように記述します。

Boolean doesThisAllowMultipleSelections = selectObject.isMultiple();
  
does_this_allow_multiple_selections = select_object.is_multiple
  
bool doesThisAllowMultipleSelections = selectObject.IsMultiple;
  
does_this_allow_multiple_selections = select_object.multiple?
  
// This feature is not implemented - Help us by sending a pr to implement this feature
  
val doesThisAllowMultipleSelections = selectObject.isMultiple