【问题标题】:"The method selectByValue(String) undefined for the type WebElement" in SeleniumSelenium 中的“WebElement 类型的方法 selectByValue(String) 未定义”
【发布时间】:2017-03-30 16:20:58
【问题描述】:

请有人帮我完成这个..

我想为下拉菜单编写 selenium 代码。当我尝试使用以下语法时。我在 Eclipse 中遇到此错误“WebElement 类型的方法 selectByValue(String) 未定义”。

我使用以下语法:

ele.selectByIndex(5);
ele.selectByValue("3");
ele.selectByVisibleText(Feb);

提前谢谢...

我在找到解决方案后进行了更新。 我已经编写了这样的代码供其他人使用。它对我来说很好。

public void selectCreditCardType(WebElement selectCreditCardType) throws Exception {
    logger.info("Select Card");
    browser.selectByText(selectCreditCardType, "Visa Card");
    browser.Wait(10);
}

【问题讨论】:

  • 可能你的元素ele没有value属性

标签: selenium-webdriver


【解决方案1】:

实际上selectByVisibleText(); 方法将参数作为字符串,而您将值作为 web 元素传递,这就是它显示错误的原因

只需将参数类型更改为字符串(包含在" 中)并尝试

ele.selectByVisibleText("Feb");

更新:

    @FindBy(xpath = "//select[@name='CTYPE']")
    private WebElement selectCreditCard;
    public void selectCreditCardType() throws Exception 
    {
    logger.info("Select Card");
    Select select = new Select(selectCreditCard);
    select.selectByIndex(2); 
    }

【讨论】:

  • @suresh kumar 你能替换这些Select select = new Select(driver.findElement(By.xpath("//select[@name='CTYPE']"))); select.selectByIndex(2); 并让我们知道你遇到的任何错误
  • 抱歉耽搁了...请不要介意.. purchaseInsurance.selectBankBranch(); .. 当我在测试页面中调用它时.. 在 Eclipse 中显示错误“多个标记”
  • 我想为日期选择两个下拉列表,即 MM YYYY
  • 然后你必须找到两个单独的下拉菜单,例如Select dd = new Select(driver.findElement(By.xpath("//select[@name='date']"‌​))); dd.selectByIndex(2);Select yy = new Select(driver.findElement(By.xpath("//select[@name='year']"‌​))); yy.selectByIndex(2); 像这样
【解决方案2】:

仔细查看错误信息。

“未为类型 WebElement 定义方法 selectByValue(String)”

这是说你正在做的 WebElement.selectByValue() 没有定义。 .selectByValue() 是为 Select 定义的。您没有将所有相关代码发布到您的示例中,但您可能有类似的代码

WebElement ele = driver.findElement(locator);
ele.selectByIndex(5);

这意味着eleWebElement 类型。您正在尝试在 SELECT 元素中设置一个值,因此您希望使用 Select 类。你可以这样做

WebElement ele = driver.findElement(locator);
Select select = new Select(ele);
select.selectByIndex(5);

您应该花一些时间复习Selenium docs for Java,尤其是Select 类。

【讨论】:

  • 感谢您的回复...很抱歉这么说.. 仍然面临同样的问题。我有点困惑。你能告诉我下面的代码吗.. @FindBy(xpath = "//select[@name='CTYPE']") private WebElement slctCreditCardType; public void selectCreditCardType() 抛出异常 { logger.info("Select Card");选择 select = new Select(slctCreditCardType); select.selectByIndex(2); }
  • 请在您的问题中发布代码,以便所有人都能看到它,它可以正确格式化,并发布完整的错误消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 2023-01-10
相关资源
最近更新 更多