Selecting dropdown in WebDriverJs

后端 未结 15 934
星月不相逢
星月不相逢 2020-12-29 07:48

I have a dropdown box that I would like to select a value using WebDriverJS. I\'ve looked at the user guide below and could not find out how to do it

相关标签:
15条回答
  • 2020-12-29 08:08

    This should achieved by

    selectElem = driver.findElement(webdriver.By.id("vote"))
    selectElem.click()
    selectElem.findElement(webdriver.By.css("option[value='5']")).click()
    
    0 讨论(0)
  • 2020-12-29 08:11

    I shared a function to select a drop down item by it's text here.

    The code:

    function selectOption(selector, item){
        var selectList, desiredOption;
    
        selectList = this.findElement(selector);
        selectList.click();
    
        selectList.findElements(protractor.By.tagName('option'))
            .then(function findMatchingOption(options){
                options.some(function(option){
                    option.getText().then(function doesOptionMatch(text){
                        if (item === text){
                            desiredOption = option;
                            return true;
                        }
                    });
                });
            })
            .then(function clickOption(){
                if (desiredOption){
                    desiredOption.click();
                }
            });
    }
    

    use with:

    driver.selectOption = selectOption.bind(driver);
    driver.selectOption(webdriver.By.id('my-dropdown'), 'My Value');
    
    0 讨论(0)
  • 2020-12-29 08:16

    driver.click('//*[@id="vote"]/option[3]')

    Ref: https://stackoverflow.com/a/22159563/227578

    0 讨论(0)
提交回复
热议问题