Selenium WebDriver and DropDown Boxes

前端 未结 10 1756
星月不相逢
星月不相逢 2020-11-29 00:16

If I want to select an option of a dropdown box, there are several ways to do that. I always used:

driver.findElem         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 00:46

    Just wrap your WebElement into Select Object as shown below

    Select dropdown = new Select(driver.findElement(By.id("identifier")));
    

    Once this is done you can select the required value in 3 ways. Consider an HTML file like this

    
    
    
    
    
    

    Now to identify dropdown do

    Select dropdown = new Select(driver.findElement(By.id("designation")));
    

    To select its option say 'Programmer' you can do

    dropdown.selectByVisibleText("Programmer ");
    

    or

     dropdown.selectByIndex(1);
    

    or

     dropdown.selectByValue("prog");
    

    Happy Coding :)

提交回复
热议问题