2014-12-03 105 views
4

我有一個要求獲取下拉列表的所有選項,我需要遍歷這些值並提交表單。所以我需要下拉列表的值。使用硒和nodejs獲取下拉值

下面的Java代碼符合我的確切要求,但我需要在Javascript +硒+相同的代碼的NodeJS

Select select = new Select(driver.findElement(By.name("height"))); 
List list = select.getOptions(); 

這是可能的的NodeJS +硒+ JavaScript的

+0

可能的重複:http://stackoverflow.com/questions/26041791/how-to-select-a-dropdown-value-in-selenium-webdriver-using-node-js/28643481#28643481 – 2015-02-21 07:50:52

回答

-3

你可以用做Cheerio -

module.exports = { 
    "login": function (browser) { 
     var cheerio = require("cheerio") 

     browser 
      .url("http://www.wikipedia.org") 
      .waitForElementVisible('body', 1000) 
      .waitForElementVisible('select#searchLanguage', 1000) 
      .source(function(result) { // .source() dump the page source in the variable 
       $ = cheerio.load(result.value) 
       var num_languages = $('select#searchLanguage option').length 
       console.log('Wikipedia.org Languages: ' + num_languages); 
      }) 
      .end(); 
    } 
}; 

查看this other question相同。

-1

對於任何人停留在此,繼承人如何我做到了一個選項:

<select id='mySelection'> 
    <option value='0'>Hello</option> 
    <option value='1'>Everybody</option> 
    <option value='2'>Got</option> 
    <option value='3'>It</option> 
</select> 

在硒的NodeJS,你會搶了 「它」:

var webdriver = require('selenium-webdriver'); 

var driver = new webdriver.Builder(). 
    withCapabilities(webdriver.Capabilities.chrome()). 
    build(); 

driver.findElement(webdriver.By.id('mySelection')).sendKeys('3'); 

當你.sendKeys ()的選項它必須等於值=''

在你的情況下,只需抓住父元素,然後sendKeys()爲子元素。

+0

你好GARPdev,謝謝爲你的答案,我已經嘗試過你的解決方案,但它適用於某人和某些時候它會失敗說例子在我的情況下,需要通過閱讀csv文件多個記錄在那時填寫地址表單這個解決方案將要失敗 – QAMember 2015-03-04 05:15:26

+0

嘗試使用此讀取csv? https://www.npmjs.com/package/fast-csv – GARPdev 2015-03-04 17:57:03