【问题标题】:Selenium Select not working on <select> elementSelenium Select 不适用于 <select> 元素
【发布时间】:2019-09-07 20:47:30
【问题描述】:

我的任务是点击下拉列表中的元素,完全使用 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 毫秒)

    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正确选择下拉列表元素?

【问题讨论】:

  • 尝试使用waitToBeClickable(dropListElement); 而不是waitToBePresent(dropListElement);。
  • @RatmirAsanov 我确实升级了问题
  • 你能分享页面的 URL 吗?

标签: java selenium


【解决方案1】:

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


我发布了一个我经常使用的实用函数。

    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2014-03-14
    • 2012-05-10
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多