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

Selenium WebDriver - How to identify the element from custom drop-down. DIV, UL and LI

我正在尝试自动化具有 DIV、UL 和 LI 的自定义下拉字段。我无法使用 Select class 或 CSSSelect 或动态更改。

    <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>

如果有人能帮助我解决这个问题,那将是非常有帮助的。

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

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

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

要在内部定位下拉元素,我会使用文本匹配:

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

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