Selecting dropdown in WebDriverJs

后端 未结 15 933
星月不相逢
星月不相逢 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 07:51

    This is not actually clicking the option, but it does in fact select it.

    1. Find select element
    2. Click select element
    3. Type option text into select element using sendKeys()
    4. Click select element
    0 讨论(0)
  • 2020-12-29 07:53

    I am using webdriverjs and want to select the option by index, so did:

    driver.click('#my_select_box').click('#my_select_box option:nth-child(3)')
    
    0 讨论(0)
  • 2020-12-29 07:54

    The following code defines the available selectors in WebDriverJS:

    webdriver.Locator.Strategy = {
      'className': webdriver.Locator.factory_('class name'),
      'class name': webdriver.Locator.factory_('class name'),
      'css': webdriver.Locator.factory_('css selector'),
      'id': webdriver.Locator.factory_('id'),
      'js': webdriver.Locator.factory_('js'),
      'linkText': webdriver.Locator.factory_('link text'),
      'link text': webdriver.Locator.factory_('link text'),
      'name': webdriver.Locator.factory_('name'),
      'partialLinkText': webdriver.Locator.factory_('partial link text'),
      'partial link text': webdriver.Locator.factory_('partial link text'),
      'tagName': webdriver.Locator.factory_('tag name'),
      'tag name': webdriver.Locator.factory_('tag name'),
      'xpath': webdriver.Locator.factory_('xpath')
    };
    
    goog.exportSymbol('By', webdriver.Locator.Strategy);
    

    Source: https://code.google.com/p/selenium/source/browse/javascript/webdriver/locators.js

    0 讨论(0)
  • 2020-12-29 07:55

    Find the select element and click on it to display the dropdown

    driver.findElement(//div//select[@id="elementId"]).click();

    Then select from the options and click on it. I think selecting by xpath will really work better here.

    driver.findElement(By.xpath('//div//select[@id="elementId"]//option[optionIndex]')).click();

    0 讨论(0)
  • 2020-12-29 07:58

    I was using the following with ES6:

     let select = driver.findElement(By.css("select"));
     let options = select.findElements(By.css("option"));
     options.then(values => {
         return Promise.all(values.map(el => el.getText())).then(optTexts => {
             return values[optTexts.indexOf('Some option text')].click();
         });
     });
    
    0 讨论(0)
  • 2020-12-29 08:01

    I get same problem, and above solutions not working in my typeScript case

    But I still find a solution:

    await driver.findElement(By.id("ELEMENT_ID")).sendKeys("SOME_VALUE");
    

    Due to driver will return a promise when get a selector element

    So add await to do the next things

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