Selenium WebDriver - 如何从自定义下拉列表中识别元素。 DIV,UL和LI

时间:2015-03-16 14:06:40

标签: html css selenium selenium-webdriver css-selectors

我正在尝试自动化具有DIV,UL和LI的自定义下拉字段。我无法使用Select类或CSSSelector作为动态更改。

    <div id="boundlist-1092" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-border-box x-boundlist-above" tabindex="-1" style="right: auto; left: 495px; top: 245px; height: auto; z-index: 29001; width: 150px;">
<div id="boundlist-1092-listEl" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;">
<ul class="x-list-plain">
<li class="x-boundlist-item" unselectable="on" role="option">Single</li>
<li class="x-boundlist-item" unselectable="on" role="option">Married Filing Jointly</li>
<li class="x-boundlist-item" unselectable="on" role="option">Married Filing Separately</li>
<li class="x-boundlist-item" unselectable="on" role="option">Head of Household</li>
<li class="x-boundlist-item" unselectable="on" role="option">Qualifying Widow(er)</li>
</ul>
</div>
</div>

但是,当元素可见时,HTML代码看起来不同

    <div id="boundlist-1092" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-border-box x-boundlist-above" tabindex="-1" style="right: auto; left: 495px; top: 245px; height: auto; z-index: 29001; width: 150px;">
<div id="boundlist-1092-listEl" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;">
<ul class="x-list-plain">
<li class="x-boundlist-item x-boundlist-selected" unselectable="on" role="option">Single</li>
<li class="x-boundlist-item" unselectable="on" role="option">Married Filing Jointly</li>
<li class="x-boundlist-item" unselectable="on" role="option">Married Filing Separately</li>
<li class="x-boundlist-item" unselectable="on" role="option">Head of Household</li>
<li class="x-boundlist-item" unselectable="on" role="option">Qualifying Widow(er)</li>
</ul>
</div>
</div>

如果有人可以帮助我,那将是非常有帮助的。

1 个答案:

答案 0 :(得分:1)

要找到下拉列表,您可以将部分匹配应用于XPath表达式:

//div[starts-with(@id, "boundlist-")]

我们要求获取div元素,其中id属性以boundlist-开头。

要找到内部的下拉元素,我会使用文本匹配:

//div[starts-with(@id, "boundlist-")]//li[. = "Married Filing Jointly"]

其中.指的是当前元素的文本。