Nearsoft\SeleniumClient\SelectElement::selectByValue PHP Method

selectByValue() public method

Sets an option selected by its value
public selectByValue ( String $value )
$value String
    public function selectByValue($value)
    {
        $options = $this->_element->findElements(By::xPath(".//option[@value = '" . $value . "']"));
        $matched = false;
        foreach ($options as $option) {
            if (!$option->isSelected()) {
                $option->click();
            }
            $matched = true;
        }
        if (!$matched) {
            throw new \Exception("Cannot locate option in select element with value: " . $value);
        }
    }

Usage Example

 public function testSelectByValueShouldSelect()
 {
     $select = new SelectElement($this->_driver->findElement(By::id("sel1")));
     $select->selectByValue("2");
     $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 2]"))->isSelected());
     $select = new SelectElement($this->_driver->findElement(By::id("sel2")));
     $select->selectByValue("onions");
     $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 'onions']"))->isSelected());
 }
All Usage Examples Of Nearsoft\SeleniumClient\SelectElement::selectByValue