如何从下拉列表中添加项目,然后获取每个项目的文本

时间:2022-11-27 14:27:54

I have a dropdown of 10 items. How can I write a code that would store all these items in some sort of list so that i can compare each of the items to each other? The layout of the page is:

我有10个项目的下拉列表。如何编写将所有这些项目存储在某种列表中的代码,以便我可以将每个项目相互比较?页面的布局是:

<select id="blahBlah">

 <option value = "0" selected="selected">fghgjjyg1</option>
 <option value = "1">dghdfgffg2</option>
 <option value = "2">fsd gdf3</option>
 <option value = "3">f fdgfdg4</option>
 <option value = "4">dfsgf5</option>
 <option value = "5">fdgfdsgsd6</option>
 <option value = "6">sgfgsfgdf7</option>
 <option value = "7">fdsgsdgg8</option>
 <option value = "8">fdsgfds9</option>
 <option value = "9">dfsdfs10</option>
</select>

So there are numbers and the end of each item - I want to be able to verify that the numbers on the top are smaller than the numbers below it.

所以每个项目都有数字和结尾 - 我希望能够验证顶部的数字是否小于它下面的数字。

3 个解决方案

#1


1  

You want to use Webdrivers findElements function

您想使用Webdrivers findElements函数

List<WebElement> options = driver.findElements(By.xpath(//select[@id=blahBlah]/option));
for(WebElement e : options){
    System.out.println(e.getText())
}

That's generally what you want to do. Your HTML looks a little odd so I'm not sure if the xpaths are correct or not but this should get you started.

这通常是你想要做的。你的HTML看起来有点奇怪,所以我不确定xpath是否正确但这应该让你开始。

#2


-1  

Without using jQuery:

不使用jQuery:

var selectDom  = document.getElementById("blahBlah");
var optionsDom = selectDom.getElementsByTagName("option");
var optionsArray = Array.prototype.slice.call(optionsDom);
var theArrayThatYouWant = optionsArray.map(function(option) { return option.text });

#3


-1  

Using jQuery:

使用jQuery:

var yourArray = []
$('#blahBlah option').each(function(){yourArray.push($(this).text())});

#1


1  

You want to use Webdrivers findElements function

您想使用Webdrivers findElements函数

List<WebElement> options = driver.findElements(By.xpath(//select[@id=blahBlah]/option));
for(WebElement e : options){
    System.out.println(e.getText())
}

That's generally what you want to do. Your HTML looks a little odd so I'm not sure if the xpaths are correct or not but this should get you started.

这通常是你想要做的。你的HTML看起来有点奇怪,所以我不确定xpath是否正确但这应该让你开始。

#2


-1  

Without using jQuery:

不使用jQuery:

var selectDom  = document.getElementById("blahBlah");
var optionsDom = selectDom.getElementsByTagName("option");
var optionsArray = Array.prototype.slice.call(optionsDom);
var theArrayThatYouWant = optionsArray.map(function(option) { return option.text });

#3


-1  

Using jQuery:

使用jQuery:

var yourArray = []
$('#blahBlah option').each(function(){yourArray.push($(this).text())});