2013-04-07 246 views
7

我有一個下拉框,我想使用WebDriverJS選擇一個值。我已經看了下面的用戶指南,找不到了怎麼辦呢在WebDriverJs中選擇下拉菜單

https://code.google.com/p/selenium/wiki/WebDriverJs

我甚至嘗試被證明爲Java版本是這樣幾件事情:

webdriver.Select(driver.findElement(webdriver.By.id("vote"))).selectByValue("5") 

它只是簡單地說「Select」不存在。

我經歷了源​​代碼,仍然找不到任何可以使用的東西。

回答

2

這應該

selectElem = driver.findElement(webdriver.By.id("vote")) 
selectElem.click() 
selectElem.findElement(webdriver.By.css("option[value='5']")).click() 
+0

當異步代碼(承諾和所有),這將失敗處理。 – 2016-01-28 21:20:14

3

實現我使用webdriverjs並要選擇由索引選項,這樣做的:

driver.click('#my_select_box').click('#my_select_box option:nth-child(3)') 
8

我共享的功能來選擇一個下拉項由它的文字here

的代碼:

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(); 
      } 
     }); 
} 

使用與:

driver.selectOption = selectOption.bind(driver); 
driver.selectOption(webdriver.By.id('my-dropdown'), 'My Value'); 
+0

在這裏回答! – 2016-01-28 22:20:06

+0

這個解決方案的好處是,它是通過文本進行搜索,而不是通過選項號進行搜索。 – 2016-11-02 20:17:51

-1

下面的代碼定義在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); 

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

0

這實際上並沒有點擊該選項,但它實際上選擇了它。

  1. 查找選擇元素
  2. 點擊選擇元素
  3. 類型選項文本選擇使用元素的SendKeys()
  4. 點擊選擇元素
+0

這在不同的瀏覽器上失敗,在leas我試過mac chrome,windows FF並且失敗了): – porfiriopartida 2015-10-24 19:22:55

+0

這是行不通的! Java方法在這裏是不夠的 – 2016-01-28 19:25:15

8

你不不需要點擊兩次即可等一個選項,只需直接點擊該選項即可。喜歡的東西,

driver.findElement(wd.By.css('#month>option[title=\'November\']')).click(); 
0

這會爲我(的CoffeeScript)工作

selectList.findElements(By.tagName("option")) = 
.then (options) -> 
    len = options.length   #getting number of options in the select 
    driver.wait =>    #verify all promises finished 
     for option in options 
      option.getText() 
      .then (text) => 
       console.log(len) 
       len -= 1 
       console.log(text) 
       if len is 0 
        true 
    , 10000 
2
driver.findElement({id: 'myDropDown'});// select dropdown element you wish to select 
driver.sleep(1000);//not necessary 
driver.findElement({id: 'myDropDown'}).sendKeys('name_of_option');//sending keys automatically fills dropdown with desired option 
+0

這對我有用,選擇下拉,然後發送密鑰 – 2016-10-19 16:22:22

0

我用用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

某些瀏覽器在下拉菜單時非常困難。我有一些想法,並使用JS注入拼湊了一個Java方法,可能適用於你遇到過這個問題的你。是的,這些問題正在逐步修復,但對於那些負責驗證舊版瀏覽器的人員來說,這是非常有用的。 我希望這可以幫助,因爲這可能是非常令人沮喪的!

public void getJSDropdown(String dropDown, String elementID)throws Exception{ 

    JavascriptExecutor executor = (JavascriptExecutor)driver; 
    String dropdownScript = "var select = window.document.getElementById('" + 
      dropDown + 
      "'); for(var i = 0; i < select.options.length; i++){if(select.options[i].text == '" + 
      elementID + 
      "'){ select.options[i].selected = true; } }"; 

    Thread.sleep(2000); 
    executor.executeScript(dropdownScript); 
    Thread.sleep(2000); 

    String clickScript = "if ("+"\"createEvent\""+" in document) {var evt = document.createEvent("+"\"HTMLEvents\""+");  evt.initEvent("+"\"change\""+", false, true); " + dropDown + ".dispatchEvent(evt); } else " + dropDown + ".fireEvent("+"\"onchange\""+");"; 

    executor.executeScript(clickScript); 

} 
0

使用XPath的那樣

await driver.findElement(By.xpath('//[@id="newEventOffices"]/option[3]')).click();