【问题标题】:Unable to select from dropdown无法从下拉列表中选择
【发布时间】:2016-08-09 11:19:26
【问题描述】:

我在 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");

但是我没有得到一个错误位,没有选择任何值。如何解决它。 它点击下拉菜单,并没有从该列表中选择项目。请帮助

【问题讨论】:

  • 你能分享一下确切的错误信息吗?
  • 已更新..请检查
  • 该错误看起来与下拉菜单无关。在您的 selectByInvisibleText 之后添加 system.out.println 以查看它是否真的认为它已被选中?告诉我。
  • 您的例外是找不到元素 "//*[@id='new_user_vitals']/fieldset[3]/div[2]/div/div[2] /a[3]/div/div/div[2]" 。该元素不在您共享的硒代码中,因此问题发生在此之前或之后。将此行添加到硒代码 System.out.println("Selected the option"); 的底部如果在控制台中打印出该选项,则该选项已被选中,或者至少它认为它已被选中。

标签: java selenium selenium-webdriver


【解决方案1】:

您似乎使用了错误的定位器。 试试这个:

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

【讨论】:

  • 不工作..如果我使用 id(1) 选择,我只能看到它点击 n 查看下拉菜单,但没有选择任何值
【解决方案2】:

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

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

// 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 +");

【讨论】:

  • waitForDropDownToPopulate 未定义它即将到来
【解决方案3】:

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

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

【讨论】:

  • 我不是在谈论 A 和 + 之间的空格。可见文本中“A+”前后可能有空格。试试 selectByValue("A +") 看看它是否有效。
  • 尝试不选择
  • 能否更新异常。正如您之前提到的例外是 statement.WebElement blood_grp=driver_new.findElement(By.xpath("html/body/div[2]/div/form/fieldset[5]/div[1]/select") );
  • 它现在没有抛出错误,但值没有在下拉列表中被选中
【解决方案4】:
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);
    }

【讨论】:

  • 尝试第二个选项
  • 对不起语法错误。
  • 不,这是我的错误尝试使用固定代码(在大多数情况下我使用 c# 开发)
  • 有异常吗?你在选项列表中看到对象了吗?
  • 现在它也点击并显示下拉列表但不选择
【解决方案5】:

您的代码对我来说似乎是正确的。如果不能亲自查看该站点,就很难知道发生了什么。让我们做一些调试。下面的代码应该可以工作。执行它,然后发布它正在打印的内容。我在想有什么微妙的事情发生了。

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 并看到了OPTIONs...如果您尝试下面的代码会发生什么。

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

...或者这个...

Select bloodGroup = new Select(driver.findElement(By.id("user_vitals_blood_group")));
bloodGroup.selectByIndex(3);

【讨论】:

  • 好的。我更新了答案...试试我刚刚添加的两个代码示例,让我知道会发生什么。
  • 这应该都可以。如果不亲自查看该网站,我不知道还能尝试什么或出了什么问题。
  • 请告诉我你什么时候上线会告诉网站地址
  • 我使用的是 selenium 3.0.0 beta 版本,可能是这个问题?
  • @john 我不知道。找出答案的唯一方法是回到非测试版,看看它是否有效。
猜你喜欢
  • 2016-10-13
  • 2016-04-14
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多