How to use select list in selenium?

前端 未结 4 1984
说谎
说谎 2020-12-20 12:58

I\'m trying to select an element from a select list in selenium using java with WebDriver - based syntax.

I\'ve got the select list by

    elements =         


        
4条回答
  •  萌比男神i
    2020-12-20 13:48

    WebElement select = driver.findElement(By.name("myselect"));
    Select dropDown = new Select(select);           
    String selected = dropDown.getFirstSelectedOption().getText();
    if(selected.equals(valueToSelect)){
        //already selected; 
        //do stuff
    }
    List Options = dropDown.getOptions();
    for(WebElement option:Options){
        if(option.getText().equals(valueToSelect)) {
          option.click(); //select option here;       
        }               
    }
    

    If this is slower, then consider something like

    dropDown.selectByValue(value);
    or
    
    dropDown.selectByVisibleText(text);
    

提交回复
热议问题