0

我是使用 webdriver 的初学者,这是我正在使用的代码。我想单击一个下拉列表,然后再次单击为该下拉列表提供的值列表。请帮助我,我寻找其他线程,但它们是不同的。

<ul>
  <li class="size select required">
    <div id="dk_container_partNumber-55414" class="dk_container dk_theme_size dk_focus"
    tabindex="" style="display: block;">
      <a class="dk_toggle" style="width: 162px;">
        <div class="dk_options">
          <ul class="dk_options_inner">
            <li class="dk_option_current">
              <li class="">
                <a data-dk-dropdown-value="605930243">S</a>
              </li>
              <li class="">
                <a data-dk-dropdown-value="605930251">M</a>
              </li>
              <li class="">
                <a data-dk-dropdown-value="605930260">L</a>
              </li>
              <li class="">
                <a data-dk-dropdown-value="605930278">XL</a>
              </li>
              <li class="">
                <a data-dk-dropdown-value="605930286">XXL</a>
              </li>
          </ul>
        </div>
    </div>
    <select id="partNumber-55414" class="size-select" name="partNumber" style="display: none;">
      <option data-property="CAT_SELECTSIZE" selected="selected" value="">Select Size</option>
      <option value="605930243">S</option>
      <option value="605930251">M</option>
      <option value="605930260">L</option>
      <option value="605930278">XL</option>
      <option value="605930286">XXL</option>
    </select>

我希望选择“S”,并且必须在下拉列表中看到相同的内容。我可以单击下拉菜单并使用以下代码查看选项列表:

driver.findElement(By.xpath("//(more xpath goes here)/ul/li[@class = 'size select required']/div/a/span")).click();

我尝试了以下代码来选择“S”,但徒劳无功:

Select SizeDropdown = new Select(driver.findElement(By.xpath("//(more xpath  here)/ul/li[@class = 'size select required']/select")));
//driver.findElement(By.xpath("(//(more xpath here)/ul/li[@class = 'size select required']/select)/option[2]")).click();
//Below line gets and clicks the first option of Select Size 'S'
//SizeDropdown.getFirstSelectedOption().click();
//SizeDropdown.selectByVisibleText("S");
//SizeDropdown.selectByIndex(1);
//SizeDropdown.selectByValue("S");

我分别尝试了每个命令(取消注释后),但看不到“S”作为下拉菜单的标签,就好像它是我选择的一样。

4

1 回答 1

0

您是否尝试过简化您的 xpath?

我使用类似这样的方法从下拉列表中选择一个选项,而无需先单击以展开它:

Select select = new Select(driver.findElement(By.xpath("//select[@id='partNumber-55414']"));
select.selectByVisibleText("S");
于 2013-01-31T17:55:53.463 回答