Selecting WebElements with dynamically generated ids by xpath Selenium java

蓝咒 提交于 2019-12-02 21:31:35

问题


I need to select an element in a dropdown window. Every time I open the dropdown window in the site I am testing, the website randomly generates an id for that dropdown window. Previous instances of the dropdown window are visible (using Firebug) but not selectable. There is a static path but it only works when I test it with ChromeDriver, and not when I use FirefoxDriver. Locating the dynamically generated elements by class name (each instance of the dropdown window has the same class) works the first time I try it but I get errors every time after that using both ChromeDriver and FirefoxDriver. I think it may be attempting to locate that first instance, but not the selectable instance.

Here's my code for the dynamic stuff:

driver.findElement(By.xpath("//div[@class='really long name for drop down menu']/ul/li[2]")).click();

And here's my code for the static stuff:

driver.findElement(By.xpath("//option[normalize-space(.)='Text']")).click();

And here's the dynamic HTML:

<html class="FakeName1" style="">
    <body style="margin: 0px; background-color: rgb(219, 230, 244);">
        <form id="Form1" action="MenuSelector.aspx?Ihopethisstuffisnotimportant" method="post">
        <div id="Menu1384921" class="really long name for drop down menu" style="overflow-y: auto; width: 438px; height: 320px; position: absolute; visibility: hidden; left: 165px; top: 88px; display: none;">
        <div id="Menu1092875" class="really long name for drop down menu" style="overflow-y: auto; width: 438px; height: 320px; position: absolute; visibility: visible; left: 165px; top: 88px;">
            <ul>
                <li unselectable="on"></li>
                <li unselectable="on">Text</li>
            </ul>
        </div>
    </body>
</html>

And here's the static HTML:

<div id="ThingList" style="width:100%;">
    <table id="Table1" style="margin: 0px; padding: 0px; width: 100%; border-spacing: 4px;">
        <tbody>
            <tr>
                <td align="right" style="width: 20%; font-size: 9pt;">Select a Thing: </td>
                <td>
                    <select id="bThings" class="bInput" style="width: 436px;" onchange="javascript:setTimeout('__doPostBack(\'bThings\',\'\')', 0)" name="bThings">
                        <option value=""></option>
                        <option value="2">Text</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>
</div>

回答1:


Try this approach:

Get all elements.

java.util.List<WebElement> elements = driver.findElements(By.xpath("//div[@class='really long name for drop down menu']/ul/li[2]"));
elements[elements.count - 1].click();

That should click the last element with that particular class.



来源:https://stackoverflow.com/questions/25268957/selecting-webelements-with-dynamically-generated-ids-by-xpath-selenium-java

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