如何使用Java计算Selenium WebDriver中选择下拉框中的选项数量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20210853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:20:10  来源:igfitidea点击:

How to count the number of options in a select drop down box in Selenium WebDriver using Java?

javaeclipseseleniumselenium-webdriver

提问by Jaydev

I have Select Dropdown list with this:

我有这样的选择下拉列表:

xpath //*[@id="ddlTablePay"] 

I need to count the number of options in this drop-down. Thank You

我需要计算这个下拉菜单中的选项数量。谢谢你

回答by Ajay

Use .getOptions()method and store them in a list .Then find its size.

使用.getOptions()方法并将它们存储在列表中。然后找到它的大小。

Select se = new Select(driver.findElement(By.id("select drop down locator")));

List<WebElement> l = se.getOptions();
l.size();

-Ajay

-阿杰

回答by Meena T

Use .getXpathCount()method

使用.getXpathCount()方法

int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();

回答by Sharanabasu Angadi

optionItems = Select(driver.find_element_by_xpath("//select[@id='ddlTablePay']"))
print "Total Elements " + str(len(optionItems.options))

回答by IVI4K LI

String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;

回答by Ripon Al Wasim

//To count the number of options

//计算选项的数量

Select dropDown = new Select(driver.findElement(By.id("ddlTablePay")));
List<WebElement> elementCount = dropDown.getOptions();
System.out.println("Number of items: " + elementCount.size());

//To get and print all the Options

//获取并打印所有选项

Select dropDown = new Select(driver.findElement(By.id("ddlTablePay")));
        List <WebElement> elementCount = dropDown.getOptions();
        int itemSize = elementCount.size();
        for(int i = 0; i < itemSize ; i++){
            String optionsValue = elementCount.get(i).getText();
            System.out.println(optionsValue);
        }

回答by Amarjeet Singh

Select selection= new Select(driver.findElement(By.id("Drop down id")));

Select selection= new Select(driver.findElement(By.id("Drop down id")));

int size=selection.getOptions().size();

int size=selection.getOptions().size();