如何为selenium的模态选择react选择元素

IT技术 java reactjs selenium selenium-webdriver modal-dialog
2021-05-27 16:53:29

以下是我正在尝试做的事情:项目是用 ReactJS 构建的,我使用的是带有 Java 的 Selenium WebDriver。

  1. 单击按钮(我可以这样做)
  2. 这将打开一个模态,其中有react选择组件。

我想在这个选择中选择一个元素。

HTML代码:

  <div class="row">
    <div class="col-xs-7">
      <div class="Select kpi-select is-searchable Select--single">
        <div class="Select-control">
          <span class="Select-multi-value-wrapper" id="react-select-19--value">
            <div class="Select-placeholder">Select KPI</div>
            <div class="Select-input" style="display: inline-block;">
              <input id="add-kpi-kpi-select" aria-activedescendant="react-select-19--value" aria-expanded="false" aria-haspopup="false" aria-owns="" role="combobox" value="" style="box-sizing: content-box; width: 5px;">
              <div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; overflow: scroll; white-space: pre; font-size: 14px; font-family: Roboto, Helvetica, Arial, sans-serif; font-weight: 300; font-style: normal; letter-spacing: normal; text-transform: none;"></div>
            </div>
          </span>
          <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
        </div>
      </div>
    </div>
    <div class="col-xs-5">
      <div class="Select kpi-select is-searchable Select--single">
        <div class="Select-control">
          <span class="Select-multi-value-wrapper" id="react-select-20--value">
            <div class="Select-placeholder">Select Time Period</div>
            <div class="Select-input" style="display: inline-block;">
              <input id="add-kpi-timeperiod-select" aria-activedescendant="react-select-20--value" aria-expanded="false" aria-haspopup="false" aria-owns="" role="combobox" value="" style="box-sizing: content-box; width: 5px;">
              <div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; overflow: scroll; white-space: pre; font-size: 14px; font-family: Roboto, Helvetica, Arial, sans-serif; font-weight: 300; font-style: normal; letter-spacing: normal; text-transform: none;">
              </div>
            </div>
          </span>
          <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
        </div>
      </div>
    </div>
  </div>

截屏:

在此处输入图片说明

1个回答

Java代码选择KPI

String wantedOption = "wanted KPI";

// click the down arrow to expand options
driver.findElement(By.cssSelecor("div.Select.kpi-select span.Select-arrow")).click();


// select wanted KPT
driver.findElement(By.cssSelector("div.Select.kpi-select div.Select-menu"))
    .findElement(By.xpath(String.format(".//div[text()='%s']", wantedOption)))
    .click();

通过在单击事件上添加断点来使选项保持扩展的指南:

  1. 打开 Chrome 开发者工具
  2. 切换到Sources标签
  3. 按照下图所示的步骤 在此处输入图片说明

  4. 点击Select KPI页面下拉菜单

  5. 您将看到一个新的 DOM 节点<div class="Select-menu-outer"> ,其中包含ElementTab 中的所有选项

在此处输入图片说明