如何使用硒测试选择跨度元素中的值?

vlr*_*vlr 5 java selenium webdriver web-testing

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

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"
Run Code Online (Sandbox Code Playgroud)

该元素的代码如下所示:

<span style="width: 100%" val="30" id="countVal">30</span>
Run Code Online (Sandbox Code Playgroud)

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

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

这就是它的样子:

带有伪选择元素的表单

编辑 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();
        }
Run Code Online (Sandbox Code Playgroud)

这是页面源代码如何查看此元素:

在此处输入图片说明

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

Ale*_*hko 4

所以这就是发生的事情:

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

因此,您的步骤是: 1)如果id下
没有包含 class 的行,那么您需要先单击该行。 2)之后单击特定行。selec_optiondivcountSelectdiv

因此,我将尝试展示 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()
Run Code Online (Sandbox Code Playgroud)

像这样的东西。:)