Selenium - 如果值在 <table> 标签内并且不在 html 中的 <option> 下,则从下拉列表中选择一个项目

Selenium - Selecting an item from dropdown list if the values are inside <table> tags and NOT under <option> in html

下面是我们 html 代码的一个片段,它在应用程序中显示为一个下拉列表。我无法在 Selenium 中使用 Select class 从下拉列表中 select 特定值 - 可能是因为它没有 "Option" 标签?有什么方法可以select这个项目吗?

-更新:这有一个讨论可见性的父标签。基本上告诉元素只有在用户单击下拉箭头时才可见。 "<输入类型="hidden" *****"

例如我需要在测试执行期间从列表中 select 'I am option2'。

<div id="xyz" class="DropdownInnerContainer" style="z-index:99999;">
<table id="abc" class="DropdownItemContainer" list="1000020">
    <tr class="">
        <td value="" index="0" title="">&nbsp;
        </td>
        <td class="BorderCell"></td>
    </tr>
    <tr class="">
        <td value="I am option1" index="1" plid="1002827">I am option1</td>
        <td class="BorderCell"></td>
    </tr>
    <tr class="">
        <td value="I am option2" index="2" plid="1002828">I am option2</td>
        <td class="BorderCell"></td>
    </tr>
    <tr class="">
        <td value="I am option3" index="3" plid="1002829">I am option3</td>
        <td class="BorderCell"></td>
    </tr>
    <tr class="">
        <td value="I am option4" index="4" plid="1002830">I am option4</td>
        <td class="BorderCell"></td>
    </tr>
</table>

如果你想要select的td里面的文字是唯一的,那么你可以点击id为'abc'的table元素,然后点击下面的元素.提供的代码是 C#,但可以很容易地翻译。

IWebElement option = _driver.FindElement(By.XPath("//td[text()='I am option2']"));

似乎由于下拉选项位于 < table > 内,Select class 无法识别列表选项。所以这就是我所做的:

首先单击 () 下拉菜单,打开菜单:

driver.findElement(By.xpath(".//*[@id='abc01_tbl']/div/div")).click();

然后使用 contains() 方法传递值,然后在其上单击 ()。

driver.findElement(By.xpath(".//*[@id='xyz01_tbl']/tbody/tr/td[1][contains(text(),'I am option2')]")).click();

您不能在这种情况下使用 Select,因为下拉菜单没有任何 select 标记。下拉菜单位于 table 正文下方。

select 选项表单下拉列表请使用下面的 xpath。

driver.findElement(By.xpath("//table[@id='abc']/tr[td[text()='your option text']]/td"));