硒选择不适用于<select>元素

时间:2019-09-07 20:47:30

标签: java selenium

我的任务是使用完全Select类单击下拉列表中的元素。

所以我有HTML块:

<div id="_desktop_currency_selector">
  <div class="currency-selector dropdown js-dropdown open">
    <span>Валюта:</span>
    <span class="expand-more _gray-darker hidden-sm-down" data-toggle="dropdown">UAH ₴</span>
    <a data-target="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" class="hidden-sm-down">
      <i class="material-icons expand-more"></i>
    </a>
    <ul class="dropdown-menu hidden-sm-down" aria-labelledby="dLabel" style="display: block;">
        <li>
          <a title="Евро" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=2" class="dropdown-item">EUR €</a>
        </li>
        <li class="current">
          <a title="Украинская гривна" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=1" class="dropdown-item">UAH ₴</a>
        </li>
        <li>
          <a title="Доллар США" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=3" class="dropdown-item">USD $</a>
        </li>
    </ul>
    <select class="link hidden-md-up">
              <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=2">EUR €</option>
              <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=1" selected="selected">UAH ₴</option>
              <option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=3">USD $</option>
    </select>
  </div>
</div>

在尝试中,我有

  

org.openqa.selenium.TimeoutException:预期条件失败:   等待元素可点击:By.xpath:   // * [@ id ='_ desktop_currency_selector'] //选择(尝试30秒   间隔为500 MILLISECONDS)

    click(currencyDropListBtn);

    WebElement dropListBtn = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//i"));
    waitToBeClickable(dropListBtn);
    dropListBtn.click();

    WebElement dropListElement = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//select"));

    waitToBeClickable(dropListElement);
    Select select = new Select(dropListElement);
    select.selectByIndex(1);

它将按照以下方式工作:

    WebElement dropListBtn = driver.findElement(By.xpath("//*[@id='_desktop_currency_selector']//i"));
    waitToBeClickable(dropListBtn);
    dropListBtn.click();

    WebElement dropListElement = driver.findElement(By.xpath("//a[@title='Евро']"));
    waitToBeClickable(dropListElement);
    click(dropListElement);

但是我需要完全使用Select类。

如何通过Select正确选择下拉列表元素?

1 个答案:

答案 0 :(得分:1)

通常对于选择元素,您无需单击其中的选项。 您只需将select元素的值设置为要选择的选项的值即可。


我发布了一个我经常使用的实用程序功能。

    set_value("//select[@class='link hidden-md-up']",'http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&amp;id_currency=3','value')

    def set_value(xpath, val, field):
        script = """(function() 
                        {
                            node = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                            if (node==null) 
                                return '';
                            node.%s='%s'; 
                            return 'ok';
                })()"""%(xpath,field,val)
        driver.execute_script(script)