无法从下拉列表中进行选择

时间:2016-08-09 11:19:27

标签: java selenium selenium-webdriver

我在HTML下面的代码中有下拉列表

我在selenium中编写了以下代码

WebElement grp = driver_new.findElement(By.xpath("html/body/div[2]/div/form/fieldset[5]/div[1]/select"));
Select se = new Select(grp);
se.selectByVisibleText("Aa");

但我没有错误地选择任何值。如何解决它。 它点击下拉列表并且不从该列表中选择项目。请帮助

5 个答案:

答案 0 :(得分:0)

看起来你使用了错误的定位器。 试试这个:

    WebElement blood_grp=driver_new.findElement(By.name("user_vitals[blood_group]"));
    Select se=new Select(blood_grp);
    se.selectByVisibleText("A +");

答案 1 :(得分:0)

Select dropdown = new Select(driver.findElement(By.id("user_vitals_blood_group")));

选择选项O +你可以这样做:

dropdown.selectByVisibleText("O +");

或循环播放并按索引选择

dropdown.selectByIndex(i);

另一种方法:

try{
    List<WebElement> options = driver.findElement(By.id("user_vitals_blood_group")).findElements(By.tagName("option"));

    for (int i = 0; i < options.Size; i++)
    {
       new Select(driver.findElement(By.id("user_vitals_blood_group"))).selectByIndex(i);
    }

    } catch(Exception e) {
        System.out.println(e);
    }

答案 2 :(得分:0)

这里只是一个猜测,因为我不知道您收到的确切错误消息,但可能是下载尚未加载的情况。在这种情况下,我建议您使用等待下载选项加载。

这是一个等待我使用的下拉加载方法的示例 -

// Method name, pass in the Select element named "dropDown" below
public void waitForDropDownPopulate(WebDriver driver, final Select dropDown) {
    WebDriverWait wait = new WebDriverWait(driver, 30);
    try {
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                List<WebElement> list = dropDown.getOptions(); // Get options in the list 
                //System.out.println("THE LIST SIZE IS: " +list.size());
                if (list.size() < 4) { // if the total items in the list is less than 4 - do nothing
                    //System.out.println("THIS LIST RETURNED FALSE");
                    return false;
                } else {
                    //System.out.println("THIS LIST RETURNED TRUE"); // once it's great than 4 allow the script to continue 
                    return true;
                }
            }
        });
    } catch(Exception e) {
        System.out.println(e);
    }
}

所以你的代码现在应该如下所示:

WebElement blood_grp=driver_new.findElement(By.name("user_vitals[blood_group]"));
Select se=new Select(blood_grp);
className.waitForDropDownToPopulate(driver, se);
se.selectByVisibleText("A +");

答案 3 :(得分:0)

有时,可见文本有一些额外的空格,不允许选择值。请尝试以下代码来选择该选项。

 WebElement blood_grp=driver_new.findElement(By.id("user_vitals_blood_group"));
 Select se=new Select(blood_grp);
 se.selectByValue("A +");

答案 4 :(得分:0)

您的代码对我来说似乎是正确的。如果没有能够自己看到网站,很难说清楚发生了什么。我们来做一些调试。下面的代码应该有效。执行它,然后发布它正在打印的内容。我在想有一些微妙的事情发生。

Locale locale = Locale.forLanguageTag("FR");
Context context = new Context(locale);
context.setVariable("proposition", proposition);
String content = templateEngine.process("propositionPresentationConsultant", context);

您是否尝试过以下不同选项?当你尝试它们会发生什么?

Select bloodGroup = new Select(driver.findElement(By.id("user_vitals_blood_group")));
System.out.println(bloodGroup.getOptions().size());
for (WebElement option : bloodGroup.getOptions())
{
    System.out.println(option.getAttribute("outerHTML"));
}

现在我们知道代码正在抓取正确的bloodGroup.selectByValue("A +"); bloodGroup.selectByIndex(3); 并看到SELECT ...如果您尝试以下代码会发生什么。

OPTION

......或者这个...

Select bloodGroup = new Select(driver.findElement(By.id("user_vitals_blood_group")));
bloodGroup.selectByValue("A +");
相关问题