org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应已被“选择".但“跨度"为在选择下拉值时 [英] org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span" while selecting a dropdown value
问题描述
在这里,我试图使用Selenium脚本从下拉列表中选择一个值,但是在控制台中却出现了此错误,例如
Here I am trying to select a value from dropdown using selenium script but I got this error in the console like
线程主"中的异常" org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应已被选择",但已被跨度".
"Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "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);
下拉菜单中有多个值,我需要在其中选择一个值.
there are multiple values in a dropdown I need to select one value in that..
推荐答案
此错误消息...
"Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"
...表示您已使用Select
类与所需元素进行交互,因为该元素是<span>
.
...implies that you have used Select
class to interact with the desired element where as the element was a <span>
.
要选择一个值,例如使用下拉菜单中的用户名 您可以使用以下解决方案:
To select a value e.g. User Name from the dropdown using Selenium you can use the following solution:
-
代码块:
Code Block:
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();
浏览器快照:
Browser Snapshot:
注意:
- 在页面更改(即DOM更改)时尝试
click()
方法之前,始终为elementToBeClickable()
引发 WebDriverWait . - 在此特定用例中,当您浏览到所需页面时,您需要为[c4>引入 WebDriverWait 然后调用所需的
click()
的覆盖.
- Always induce WebDriverWait for the
elementToBeClickable()
before attemptingclick()
method when the page changes i.e. DOM changes. - In this particular usecase, when you browse to the desired page there is an overlay for which you need to induce WebDriverWait for the
invisibilityOfElementLocated()
and then invoke the requiredclick()
.
这篇关于org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应已被“选择".但“跨度"为在选择下拉值时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!