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



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

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

例如,我需要在测试执行期间从列表中选择"我是选项2"。

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

如果要选择的 td 中的文本是唯一的,则可以单击 id 为"abc"的表元素,然后单击以下元素。提供的代码是 C#,但可以很容易地翻译。

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

由于下拉选项位于<表>内,因此 Select 类无法识别列表选项。这就是我所做的:

第一个 click(( 下拉菜单,打开菜单:

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 标记。下拉列表位于表正文下方。

请使用下面的 xpath 作为选择选项表单下拉菜单。

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

最新更新