0

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>
jani
  • 107
  • 1
  • 5
  • 13
  • the following: "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." sounds terrible. This sounds like a very bad application bug that could cause performance problems as well. Creating a new instance of an object without removing the old one every time something is clicked is just plain bad. Are you able to get this fixed or are you stuck with the implementation as is? – mutt Aug 12 '14 at 19:02
  • It is a very troublesome implementation and yes, I am stuck with it for now. It does remove old instances when you close the iframe. The obvious solution would be to close the iframe between each test requiring the use of the dropdown. Unfortunately (EXTREMELY unfortunately), all dropdowns in that iframe have the same class name, so any test that requires more than one dropdown fails when I use the dynamic version of the xpath. Yes, I'm stuck with that too. – jani Aug 12 '14 at 19:23

1 Answers1

0

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.

mutt
  • 783
  • 1
  • 7
  • 11