通过xpath Selenium java选择动态生成的ID的Web元素 [英] Selecting WebElements with dynamically generated ids by xpath Selenium java

查看:135
本文介绍了通过xpath Selenium java选择动态生成的ID的Web元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在下拉窗口中选择一个元素。每当我打开我正在测试的网站的下拉窗口时,网站会随机为该下拉窗口生成一个ID。下拉窗口的先前实例是可见的(使用Firebug)但不可选。有一个静态路径,但只有在我使用ChromeDriver测试它时才有效,而不是在我使用FirefoxDriver时。通过类名定位动态生成的元素(下拉窗口的每个实例具有相同的类)在我第一次尝试时起作用,但每次使用ChromeDriver和FirefoxDriver后我都会收到错误。我认为它可能试图找到第一个实例,但不是可选择的实例。

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.

这是我的动态代码:

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

这是我的静态代码:

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

这里是动态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>

这里是静态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>


推荐答案

尝试这种方法:

获取所有元素。

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.

这篇关于通过xpath Selenium java选择动态生成的ID的Web元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆