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

a 夏天 提交于 2021-02-10 18:17:08

问题


The below is a snippet from our html code which appears as a drop down list in the application. I am unable to select a particular value from the drop down list using Select class in Selenium - possibly because it doesn't have "Option" tags?. Is there any way to select the item?

-UPDATE: This has a parent tag which talks about visibility. Basically to tell that elements are visible only if the user clicks the drop down arrow. "< input type="hidden" *****"

For e.g. I need to select 'I am option2' from the list during the test execution.

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


回答1:


If the text inside of the td that you want to select is unique, then you can click the table element with the id of 'abc' and then click the following element. The code provided is C#, but can be translated pretty easily.

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



回答2:


It appears that since the drop down options were inside a < table >, the Select class was unable to identify the list options. So here's what I did:

First click() the dropdown, which opens up the menu:

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

Then pass the value using the contains() method and then click() on it.

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



回答3:


You can not use Select in this scenario because there is no any select tag for dropdown. Drop down is under table body.

Please use below xpath for select option form drop down.

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


来源:https://stackoverflow.com/questions/49058624/selenium-selecting-an-item-from-dropdown-list-if-the-values-are-inside-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!