【问题标题】:How can I select the value from dropdown list where I need to choose value from drop-down list only?如何从下拉列表中选择值,而我只需要从下拉列表中选择值?
【发布时间】:2015-06-03 09:12:31
【问题描述】:

就我而言,我只能从下拉列表中选择值。 发送密钥,因为“Goibibo”与我从下拉列表中选择的不同。 那你能帮我选择值吗?

<input id="companyQuery" class="shadow-in tooltips fileSimilar" type="text" value="" onkeypress="javascript:populateCompanies();" data-name="company name" onfocus="if (this.value == 'Company Name (Complaint Against)') {this.value = '';}" name="complaintUserDTO.companyName" data-placement="right" rel="popover" placeholder="Company name" autocomplete="off" style="border-color: rgb(204, 204, 204); border-width: 1px; border-style: solid;">

GeneralMethods.driver.findElement(By.id("companyQuery")).sendKeys("Goibibo");  
            Select dropdown = new Select (GeneralMethods.driver.findElement(By.id("companyQuery")));
            dropdown.selectByValue("Goibibo");

我尝试使用 Select 类选择元素,但没有成功

【问题讨论】:

  • 您尝试过的内容,请分享该下拉列表的 html 代码部分。
  • @HelpingHands 我已经编辑了我的问题,共享了 html 代码。
  • 是 javascript 弹出窗口中的下拉菜单吗?
  • 请复制下拉菜单的html代码。我认为 JS 仅在您输入内容后才将其添加到 DOM。

标签: java selenium


【解决方案1】:

您使用 selectByValue() 选择下拉选项,但在 html 代码中该值为空。所以请使用 selectByVisibleText() 而不是 SelectByValue()。

使用下面的代码行:

 WebElement element = driver.findElement(By.id("companyQuery"));
 Select selectOption= new Select(element);
 selectOption.selectByVisibleText("Goibibo");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2018-12-16
    相关资源
    最近更新 更多