5

我要测试的页面有一个 span 元素,它实际上用作下拉选择菜单。“选择”元素的 Selenium 代码不起作用并抛出以下内容:

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"

该元素的代码如下所示:

<span style="width: 100%" val="30" id="countVal">30</span>

我打开下拉菜单时的代码是:

<tr onclick="selectNewCount(1);" class="selec_option">
<td onmouseout="blankit(this)" onmouseover="colorit(this)" class="bones_pointer out_color" id="tdgroup1">50</td>
</tr>

这是这样的:

带有伪选择元素的表单

编辑1:

这是我的硒代码:

            // choose number of records.
        try {
            WebDriverWait wait = new WebDriverWait(driver, /*seconds=*/10);

            element = wait.until(presenceOfElementLocated(By.id("countVal")));

            Select select = new Select(element);
            select.deselectAll();
            select.selectByVisibleText("100");

        } catch (NoSuchElementException ex) {
            System.out.println("PAGE SOURCE: \n" + driver.getPageSource());
            ex.printStackTrace();
        }

这是页面源代码在此元素周围的外观:

在此处输入图像描述

如果需要,我可以添加更多详细信息。谢谢。

4

4 回答 4

4

所以这就是正在发生的事情:

当您单击<div id="countSelect"></div>JavaScript时show_countSelector(),将执行并将值附加到表中。那些“选择选项”实际上是“表格行”。

所以你的步骤是:
1)如果没有带有 id类selec_option的行,那么你需要先点击它。 2) 之后,您单击特定行。divcountSelectdiv

因此,我将尝试展示 Java 代码(但是,我使用 Python 处理 Selenium):

WebElement selectorElement = driver.find(By.xpath('//*[@id="countSelect"]')));

WebElement elementOfInterest;
try {
    //trying to get the "select option"
    elementOfInterest = selectorElement.findElement(By.xpath('//*[contains(@class,"selec_option")]/td[@text()="50"]'))

} catch (NoSuchElementException e) { 
    //so options are not visible, meaning we need to click first
    selectorElement.click()
    //good idea would be to put "wait for element" here
    elementOfInterest = selectorElement.findElement(By.xpath('//*[contains(@class,"selec_option")]/td[@text()="50"]'))
}
//this would select the option
elementOfInterest.click()

像这样的东西。:)

于 2012-12-21T15:17:24.600 回答
0

你试过 selectByValue("value")吗?

于 2012-12-21T04:34:58.353 回答
0

那是因为它不是select即使看起来像...

你必须使用另一种方法。我认为一个简单的

element = driver.find(By.id("countVal"));
element.click()

应该管用

您必须找到选择工作的方式,后面应该涉及一些javascript。当您知道元素的变化是如何出现的时,您将能够找到在 Selenium 中可以做什么。但这肯定不是纯粹的select

(注意:这是一个未经测试甚至可能无法编译的代码,但它向您展示了我的观点)

于 2012-12-21T10:07:26.527 回答
0

您只能将 Select 类与 html 本机选择一起使用...您使用实际上是跨度的自定义选择。

你的例子:

 public void selectValue(String item) {
    WebElement dropDown = driver.findElement(By.id("countTd"));
    dropDown.click();

    driver.findElement(By.xpath("//td[@id='countTd']/span[text()='" + item + "']")).click();
}
于 2012-12-21T14:58:42.533 回答