【问题标题】:UnexpectedTagNameException: Element should have been "select" but was "a" while trying to get the texts of dropdown menu using Selenium and JavaUnexpectedTagNameException:元素应该是“select”,但在尝试使用 Selenium 和 Java 获取下拉菜单的文本时是“a”
【发布时间】:2022-02-10 05:53:17
【问题描述】:

在尝试获取菜单列表时,我收到以下错误消息:

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

下面是代码:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\selenium files\\chromedriver_win32_new\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.tutorialspoint.com/tutor_connect/index.php");
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    
        
        WebElement ele = driver.findElement(By.xpath("//*[@id=\"logo-menu\"]/div/div[1]/div/a"));
        
        Select s = new Select(ele);
        
    //getting list of menu
         List <WebElement> op = s.getOptions();
          int size = op.size();
          for(int i =0; i<size ; i++){
             String options = op.get(i).getText();
             System.out.println(options);
          }
    }
}

【问题讨论】:

    标签: java selenium xpath drop-down-menu css-selectors


    【解决方案1】:

    这是因为您尝试投射的元素是 link 标记,而不是 select 标记。 您需要提供正确 Select 元素的 Xpath 或 CSS,然后将其从 WebElement 转换为 Select ojbect。

    在您使用的示例中,没有真正的选择器,您首先需要单击“类别”按钮,然后选择出现的选项:

    WebElement button = driver.findElementByCSS("div[class='mui-dropdown']");
    button.click();
    WebElement SelectObj = driver.findElementByCSS("ul[class*='mui--is-open']");
    Select s = new Select(SelectObj);
    

    【讨论】:

      【解决方案2】:

      所需的元素不是&lt;select&gt; 元素,而是&lt;ul&gt; 元素。一旦你点击 &lt;a&gt; 元素,那么只有 classname mui--is-open 会附加到所需的 &lt;ul&gt; 元素。


      解决方案

      因此,要获取下拉菜单的内容,您需要将WebDriverWait 诱导为visibilityOfAllElementsLocatedBy(),您可以使用Java8 stream()map(),您可以使用其中任何一个关注Locator Strategies

      • 使用cssSelector

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.mui-btn.mui-btn--primary.categories"))).click();
        System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul.mui-dropdown__menu.cat-menu.mui--is-open a"))).stream().map(element->element.getText()).collect(Collectors.toList()));
        
      • 使用xpath

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='mui-btn mui-btn--primary categories']"))).click();
        System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='mui-dropdown__menu cat-menu mui--is-open']//a"))).stream().map(element->element.getText()).collect(Collectors.toList()));
        

      参考文献

      您可以在以下位置找到一些相关的详细讨论:

      【讨论】:

        猜你喜欢
        • 2019-08-25
        • 1970-01-01
        • 2021-08-30
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2021-10-30
        • 2020-05-26
        • 1970-01-01
        相关资源
        最近更新 更多