我想获取文本,但我得到了?(值),无法点击。这是selenium中的下拉按钮(默认选择值)

时间:2018-06-04 05:50:38

标签: html selenium

<div class="select-wrapper initialized">
  <span class="caret">▼</span>
  <input type="text" class="select-dropdown" readonly="true" 
        data-activates="select-options-56a6924e-42d9-1f78-1b82-fe2c1cbdfbdd"
        value="R3PORTS, INC">
  <select id="comp_drpdwn" class="initialized"
         data-select-id="ff6db37d-572c-bae4-7ba8-49856f516da7">
     <option value="0">Select Company</option>
     <option value="Vault Brewing Company~TI41305172">Vault Brewing Company</option>
     <option value="Vegan Picnic~TI28950835">Vegan Picnic</option>
     <option value="Yes Hospitality Group~TI09460957">Yes Hospitality Group</option>
  </select>
</div>

我的代码是

List<WebElement> out = driver.findElements(
                                By.xpath("//div[@class='select-wrapper initialized']"));
for(WebElement tuo : out) {
    String test = tuo.getText();
    System.out.println(test);
}

测试结果通过但我只有?(问号)。请帮我解决我的问题

2 个答案:

答案 0 :(得分:0)

您给定的HTML代码错过了部分选项,请在您的问题中找到更新。

String option2Select = "Vegan Picnic";

WebElement selectWrapper = driver.findElement(
            By.cssSelector("div.select-wrapper.initialized"));

// click on the ▼ to expand options
selectWrapper.findElement(By.cssSelector("span.caret")).click();

// click on wanted option
selectWrapper.findElement(By.xpath(".//option[text='" + option2Select + "']")).click()

如果上述代码无效,请尝试以下代码:

String option2Select = "Vegan Picnic";
driver.findElement(By.cssSelector("select#comp_drpdwn"));
      .findElement(By.xpath(".//option[text='" + option2Select + "']")).click()

答案 1 :(得分:-1)

您可以使用评论中提到的选择下拉列表

mb_substr()