jumper423 / yii2-php-webdriver-bindings

Automatically exported from code.google.com/p/php-webdriver-bindings
1 stars 0 forks source link

Not able to select value from dropdown #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Try to select a value from a dropdown

$list=Array();
                $select=$this->wdriver->findElementBy(LocatorStrategy::id, "lead_source");
                $list=$select->findElementsBy(LocatorStrategy::tagName, "option");
                foreach ($list as $op){
                    $txtval=$op->getText();
                    echo $txtval;
                    echo "\n";
                    if ((strcasecmp("Self Generated", $txtval))==0)
                    {
                        $op->click();
                        break;
                    }
                }   

Using the above code not able to select value from a dropdown

Original issue reported on code.google.com by selfte...@gmail.com on 25 Aug 2011 at 12:09

GoogleCodeExporter commented 8 years ago
Please note that using getText() method in foreach loop invokes remote method 
in selenium server which is very bad idea. For a combobox with more than 100 
option items it can take a few seconds. 
I recommend to select option element using xpath.  
You have 2 useful methods in WebElement class: 
    /**
     * find OPTION by text in combobox
     * 
     */
    public function findOptionElementByText($text) {
        $option = $this->findElementBy(LocatorStrategy::xpath, 'option[normalize-space(text())="'.$text.'"]');
        return $option;
    }

    /**
     * find OPTION by value in combobox
     * 
     */
    public function findOptionElementByValue($val) {
        $option = $this->findElementBy(LocatorStrategy::xpath, 'option[@value="'.$val.'"]');
        return $option;
    }

in CWebDriverTestCase.php you have method doing everything:
    public function select( $select_id, $option_text ) {
        $element = $this->getElement( LocatorStrategy::id, $select_id );
        $option = $element->findOptionElementByText( $option_text );
        $option->click();
    }

Original comment by lukasz.k...@gmail.com on 1 Sep 2011 at 3:08

GoogleCodeExporter commented 8 years ago

Original comment by lukasz.k...@gmail.com on 1 Sep 2011 at 3:12

GoogleCodeExporter commented 8 years ago

Original comment by lukasz.k...@gmail.com on 1 Sep 2011 at 3:12