【问题标题】:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span" while selecting a dropdown valueorg.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“选择”,但在选择下拉值时是“跨度”
【发布时间】:2019-06-06 04:45:26
【问题描述】:

在这里,我尝试使用 selenium 脚本从下拉列表中选择一个值,但我在控制台中收到此错误,例如

“线程“main”中的异常 org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”但是“span”..

public class HomeUserManagement {

public static void main(String args[]) {
    System.setProperty("webdriver.chrome.driver", 
"C:\\Users\\UMASHANKAR\\Documents\\selenuim\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();

//for login
    driver.get("https://ecabportal.azurewebsites.net/dashboard");

driver.findElement(By.name("email")).sendKeys("abc@xyz.in");

driver.findElement(By.name("password")).sendKeys("abc123xyz");
    driver.findElement(By.name("signIn")).click();  


//actual code for selecting a value from dropdown

 driver.get("https://ecabportal.azurewebsites.net/user");
    Select drpdwn=new Select(driver.findElement(By.id("select2-signup-username-container")));
    drpdwn.selectByVisibleText("User Name");
    drpdwn.selectByIndex(0);

下拉列表中有多个值,我需要在其中选择一个值..

【问题讨论】:

  • 谁能帮帮我
  • 你能发布(最少的)HTML 来重现吗?
  • @orde OP 提供了 URL...不需要 HTML...
  • @MosheSlavin 问题中需要 HTML。明天网站设计可能会改变,这个问题对未来的读者毫无用处。相关的 HTML 应始终包含在问题中。话虽如此,如果 OP 对“相关”HTML 的定义并不是真正需要的,另外添加页面链接很多时候会很有帮助。

标签: java selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

如错误所示,您使用的是<span> 标签而不是Select

您要查找的Select 元素是//*[@id="signup-username"]

另外,您应该使用WebDriverWait 等待您的定位器:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

您应该查看ExpectedConditions 以等待...

希望对你有帮助!

【讨论】:

  • 如果我使用这个代码..我会得到像这样的异常“线程“主”org.openqa.selenium.ElementNotInteractableException:元素不可交互”
  • 他们用于下拉菜单的这个 html 代码 选择
【解决方案2】:

此错误消息...

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

...暗示您已使用Select 类与所需元素进行交互,因为该元素是&lt;span&gt;

选择一个值,例如用户名从下拉列表中使用Selenium,您可以使用以下解决方案:

  • 代码块:

      driver.get("https://ecabportal.azurewebsites.net/dashboard");
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("email"))).sendKeys("admin@malbork.in");
      driver.findElement(By.name("password")).sendKeys("NsSaNj@0205");
      driver.findElement(By.name("signIn")).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Dashboard')]")));
      driver.get("https://ecabportal.azurewebsites.net/user");
      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@id='load']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.select2-selection.select2-selection--single>span.select2-selection__rendered"))).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='select2-results']//li[contains(., 'User Name')]"))).click();
    
  • 浏览器快照:


注意:

  • 当页面发生变化(即 DOM 变化)时,在尝试 click() 方法之前,始终为 elementToBeClickable() 引入 WebDriverWait
  • 在此特定用例中,当您浏览到所需页面时,您需要为 invisibilityOfElementLocated() 诱导 WebDriverWait 覆盖,然后调用所需的 click()

【讨论】:

  • 是的,它的工作原理,但我不知道它是如何工作的,你如何使用 xpath 等等,你能解释一下吗
  • @divyau 查看更新后的答案,其中包含解释。如果有任何疑问,请告诉我。
  • 你能重复一下你的 Xpath 相关问题吗?
  • new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Dashboard')]")));这个我不知道你是怎么拍的,它是哪个元素
  • 根据您的用例,您可能首先不需要访问 url https://ecabportal.azurewebsites.net/dashboard。尽管如此,为了保留您的代码试验,我确实调用了 url https://ecabportal.azurewebsites.net/dashboard,在该页面上执行任何任务之前,您需要一个 WebDriverWait。因此,在对您的用例没有任何可见性的情况下,我为标题元素引入了一个 waiter,其文本为 Dashboard
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 2011-05-07
  • 2015-05-29
相关资源
最近更新 更多