当元素不是选择类型输入时,如何使用Selenium Webdriver Java从下拉列表中获取所有选项值?

时间:2018-10-03 21:48:14

标签: java selenium-webdriver

在Salesforce应用程序中。所有元素都没有正确的静态ID或名称或其他属性。

我的要求是从salesforce网络应用程序的下拉列表中获取所有值。

在chrome浏览器上的检查下拉列表中显示CSS:

<div data-aura-rendered-by="9529:0"><a aria-required="false" class="select" aria-disabled="false" aria-describedby="9520:0-label" aria-haspopup="true" tabindex="0" role="button" title="" data-aura-rendered-by="9530:0" href="javascript:void(0);" data-interactive-lib-uid="10">--None--</a></div>'

它仅显示已选择值。即默认情况下,我的应用程序中没有。

我在此div中看不到其他选项。这无助于我获取价值。 检查下拉列表中的值时,它会在其他位置显示html

<div class="select-options" role="menu" data-aura-rendered-by="9542:0"><!--render facet: 9543:0--><ul class="scrollable" role="presentation" data-aura-rendered-by="9544:0"><!--render facet: 10751:0--><li role="presentation" data-aura-rendered-by="10755:0" class="uiMenuItem uiRadioMenuItem" data-aura-class="uiMenuItem uiRadioMenuItem"><a data-aura-rendered-by="10756:0" href="javascript:void(0);" role="menuitemradio" aria-disabled="false" tabindex="0" title="--None--" aria-checked="false"><b></b>--None--</a></li><li role="presentation" data-aura-rendered-by="10761:0" class="uiMenuItem uiRadioMenuItem" data-aura-class="uiMenuItem uiRadioMenuItem"><a data-aura-rendered-by="10762:0" href="javascript:void(0);" role="menuitemradio" aria-disabled="false" tabindex="0" title="Provider Concern / Question" aria-checked="false"><b></b>Provider Concern / Question</a></li><li role="presentation" data-aura-rendered-by="10767:0" class="uiMenuItem uiRadioMenuItem" data-aura-class="uiMenuItem uiRadioMenuItem"><a data-aura-rendered-by="10768:0" href="javascript:void(0);" role="menuitemradio" aria-disabled="false" tabindex="0" title="Provider No Show" aria-checked="false"><b></b>Provider No Show</a></li></ul></div>

我试图使用以下代码检索值。但只会得到空值:

List<WebElement> subStatuses = driver.findElements(By.xpath("(.//div[@class='select-options popupTargetContainer uiPopupTarget uiMenuList uiMenuList--default uiMenuList--left uiMenuList--short'])[3]//li"));      
        System.out.println("size of the data list :: "+subStatuses.size()+"values:: "+subStatuses.toString());              
        for(WebElement e:subStatuses)
        {
            System.out.println("Values from Fsl dropdownlist"+e.getText().toString());
        }

感谢您提供任何有关如何从此下拉列表中检索值的帮助。.

1 个答案:

答案 0 :(得分:1)

Your xpath seems to be wrong: Try something like that:

List<WebElement> subStatuses = driver.findElements(By.xpath("//div[@class='select-options']/ul/li[contains(@class, 'uiMenuItem')]/a"));

That should work if it's the only "select" on your page. If there is more than one you might need to specify which select-options-div in the dom you want e.g. selecting the first occurrence: //div[@class='select-options'][1]/ul/li[contains(@class, 'uiMenuItem')]/a and instead of getText(); you might have to use element.getAttribute('textContent');