如何使用selenium java验证下拉列表中是否存在元素/文本

如何使用selenium java验证下拉列表中是否存在元素/文本,selenium,Selenium,如何使用selenium java验证下拉列表中是否存在元素/文本 new Select(driver.findElement(By.id("airlineid"))).selectByVisibleText("Delta"); 这可以通过使用Select类来完成。 要获取可见的选定选项,请执行以下操作: WebElement select = driver.findElement(By.id("airlineid")); WebElement option = select.getFirst

如何使用selenium java验证下拉列表中是否存在元素/文本

new Select(driver.findElement(By.id("airlineid"))).selectByVisibleText("Delta");

这可以通过使用
Select
类来完成。 要获取可见的选定选项,请执行以下操作:

WebElement select = driver.findElement(By.id("airlineid"));
WebElement option = select.getFirstSelectedOption();
String selectedValueInDropDown = option.getText();
要获取所有选项,请执行以下操作:

Boolean found = false;

WebElement element = driver.findElement(By.id("..."));
Select select = new Select(element);
List<WebElement> allOptions = select.getOptions();
for(int i=0; i<allOptions.size(); i++) {
    if(alloptions[i].Equals("your_option_text")) {
        found=true;
        break;
    }
}
if(found) {
    System.out.println("Value exists");
}
Boolean-found=false;
WebElement=driver.findElement(By.id(“…”);
选择=新选择(元素);
List allOptions=select.getOptions();

对于(int i=0;i这可以通过使用
Select
类来完成。 要获取可见的选定选项,请执行以下操作:

WebElement select = driver.findElement(By.id("airlineid"));
WebElement option = select.getFirstSelectedOption();
String selectedValueInDropDown = option.getText();
要获取所有选项,请执行以下操作:

Boolean found = false;

WebElement element = driver.findElement(By.id("..."));
Select select = new Select(element);
List<WebElement> allOptions = select.getOptions();
for(int i=0; i<allOptions.size(); i++) {
    if(alloptions[i].Equals("your_option_text")) {
        found=true;
        break;
    }
}
if(found) {
    System.out.println("Value exists");
}
Boolean-found=false;
WebElement=driver.findElement(By.id(“…”);
选择=新选择(元素);
List allOptions=select.getOptions();

对于(inti=0;i我不懂java,但我可以用c#编写代码,您可以根据java方法和编码标准修改if

解决方案1

IWebElement dropDownElement = driver.FindElement(By.Id("continents"));
        if (dropDownElement.Text.Contains("the value you are looking for here"))
        {
            // value is present in drop down
        }
        else
        {
            // value is not present
        }
解决方案2

IList<IWebElement> optionsofDropDown = driver.FindElements(By.XPath("//*[@id='continents']/option"));

foreach (IWebElement optionVal in optionsofDropDown)
{
   if (optionVal.Text == "the value you are looking for here")
   {
       //value available in drop down
        break;
    }
    else
    {
       //value available in drop down
    }
  }
IList optionsofDropDown=driver.FindElements(By.XPath(“//*[@id='continents']/option”);
foreach(下拉选项中的IWebElement选项值)
{
if(optionVal.Text==“您在此处查找的值”)
{
//值在下拉列表中可用
打破
}
其他的
{
//值在下拉列表中可用
}
}

我不懂java,但我可以给你c语言的代码,你可以根据java方法和编码标准修改

解决方案1

IWebElement dropDownElement = driver.FindElement(By.Id("continents"));
        if (dropDownElement.Text.Contains("the value you are looking for here"))
        {
            // value is present in drop down
        }
        else
        {
            // value is not present
        }
解决方案2

IList<IWebElement> optionsofDropDown = driver.FindElements(By.XPath("//*[@id='continents']/option"));

foreach (IWebElement optionVal in optionsofDropDown)
{
   if (optionVal.Text == "the value you are looking for here")
   {
       //value available in drop down
        break;
    }
    else
    {
       //value available in drop down
    }
  }
IList optionsofDropDown=driver.FindElements(By.XPath(“//*[@id='continents']/option”);
foreach(下拉选项中的IWebElement选项值)
{
if(optionVal.Text==“您在此处查找的值”)
{
//值在下拉列表中可用
打破
}
其他的
{
//值在下拉列表中可用
}
}
这对我有帮助:

Select select = new Select(select_drop_down);
List<WebElement> all_options = select.getOptions();
boolean found = false;

for (WebElement we : all_options) { 
    if (facility.equalsIgnoreCase(we.getAttribute("value"))) {
        select.selectByValue(facility);
        found = true;
        break;
    }
}
Select=new Select(选择下拉菜单);
列出所有选项=select.getOptions();
布尔值=false;
对于(WebElement-we:all_-options){
if(facility.equalsIgnoreCase(we.getAttribute(“值”)){
select.selectByValue(设施);
发现=真;
打破
}
}
这对我有帮助:

Select select = new Select(select_drop_down);
List<WebElement> all_options = select.getOptions();
boolean found = false;

for (WebElement we : all_options) { 
    if (facility.equalsIgnoreCase(we.getAttribute("value"))) {
        select.selectByValue(facility);
        found = true;
        break;
    }
}
Select=new Select(选择下拉菜单);
列出所有选项=select.getOptions();
布尔值=false;
对于(WebElement-we:all_-options){
if(facility.equalsIgnoreCase(we.getAttribute(“值”)){
select.selectByValue(设施);
发现=真;
打破
}
}

感谢您的输入,我不需要检查所选的文本/值…我想知道文本/值是否在下拉列表中,然后代码的第二部分正是这样做的。如果
找到的
变量是
true
,则下拉列表中存在您的值。很高兴听到这个消息。在这种情况下,请接受答案这样,这个问题就不再是悬而未决的问题,其他人也将面临同样的问题,因为你可以看到他们可以做些什么来解决它。感谢你的输入,我不需要检查所选的文本/值…我想知道文本/值是否在下拉列表中,然后代码的第二部分正是这样做的。如果
found
变量是
true
那么您的值就存在于下拉列表中。很高兴听到您这么说。在这种情况下,请接受答案,这样这个问题就不再是悬而未决的问题,其他人将面临与您相同的问题,您可以看到他们可以做些什么来解决它。