You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Java自定义多选框选择及TestNG参数注入报错解决

Fixing Your Custom Multiselect & TestNG Issues in Selenium Java

Let's break down your problems and solve them step by step:

1. Resolving the TestNG Exception

The error org.testng.TestNGException: Cannot inject @Test annotated Method [chooseCatagory] with [class [Ljava.lang.String;] pops up because TestNG can't automatically pass the String... arguments to your annotated test method by default. Here are two straightforward fixes:

Option 1: Use a Data Provider

Define a data provider to supply your category values, then link it to your test method:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class YourTestClass {

    @DataProvider(name = "categoryData")
    public Object[][] getCategories() {
        return new Object[][] {{"ATM", "Airports"}};
    }

    @Test(description = "Test5", dataProvider = "categoryData")
    public void chooseCategory(String... categories) {
        for(String category: categories) {
            // Your selection logic here (we'll fix this next)
        }
    }
}

Option 2: Turn chooseCategory into a Helper Method

If you want to call it directly from another test method, remove the @Test annotation from chooseCategory and create a separate annotated test method to invoke it:

@Test(description = "Test5")
public void testCategorySelection() {
    chooseCategory("ATM", "Airports");
}

// Helper method (no @Test annotation)
private void chooseCategory(String... categories) {
    for(String category: categories) {
        // Your selection logic here
    }
}

2. Handling the Custom Multiselect (Native Select Class Won't Work)

The native Select class only works with HTML <select> elements. Your multiselect is a custom component (likely built with divs/ul/li, common in plugins like multiselect.js), so we need to interact with it like any other standard web element:

Key Issues in Your Original Code

  • Your CSS selector for the input box was missing the input tag, making it imprecise
  • No explicit waits, which can cause "element not found" errors if the dropdown takes time to load
  • The dropdown list selector could be more targeted to avoid matching unintended elements

Fixed Selection Logic with Explicit Waits

Here's an improved version with waits and corrected locators (adjust selectors slightly if your actual HTML uses different class names):

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

private void chooseCategory(WebDriver driver, String... categories) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

    for(String category: categories) {
        // 1. Focus the input box to open the dropdown
        WebElement inputBox = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector("div.multiselect__tags input[placeholder='Select option']")
        ));
        inputBox.click();
        inputBox.clear(); // Clear any pre-existing text
        inputBox.sendKeys(category);

        // 2. Wait for the target option to be clickable, then select it
        WebElement categoryOption = wait.until(ExpectedConditions.elementToBeClickable(
            By.xpath("//div[contains(@class, 'multiselect__content')]//li//span[text()='" + category + "']")
        ));
        categoryOption.click();

        // Optional: Verify the selection by waiting for the tag to appear
        wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.xpath("//div[contains(@class, 'multiselect__tags')]//span[text()='" + category + "']")
        ));
    }
}

Why This Works

  • Explicit Waits: Guarantees elements are fully loaded before interaction, eliminating flaky runtime errors
  • Targeted Locators: Focuses on the actual input field and dropdown content area (tweak class names if your multiselect uses different ones)
  • Confirmation Step: The optional wait for the selected tag ensures the option was successfully added to the multiselect

Final Notes

  • Double-check your HTML structure to confirm class names (like multiselect__tags, multiselect__content) match what's in your code
  • If the dropdown closes automatically after selecting an option, you may need to re-click the input box for each category
  • For deselection (if needed), you can target and click the remove icon on the selected tag elements

内容的提问来源于stack exchange,提问作者Shams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:25:18