【问题标题】:PHP-webdriver How to select third option herePHP-webdriver 如何在这里选择第三个选项
【发布时间】:2017-10-26 14:03:53
【问题描述】:

我有以下 html,我想使用 selenium PHP-webdriver 选择第三个选项,即 关闭(可选)。谁能告诉我该怎么做?

在此 HTML 中,所有 ids 都是动态生成的。这样我就不能使用 id 来查找元素,例如我不能使用这个:

$driver->findElement(WebDriverBy::id('ajax-item-ExchangeEmail-12345'));

我们可以使用 cssSelector() 或 xpath() 吗?如果是,那么如何?

谢谢。

<div id="ajax-item-12345" class="input-group" title="ExchangeEmail">
    <label class="input-group-addon" for="ajax-item-ExchangeEmail-12345">ExchangeEmail</label>
    <select id="ajax-item-ExchangeEmail-12345" class="form-control" name="category_resource[12345]">
        <option value="2">On (mandatory)</option>
        <option value="1">On (optional)</option>
        <option value="0">Off (optional)</option>
        <option value="3">Off (mandatory)</option>
    </select>
</div>

【问题讨论】:

    标签: selenium-webdriver facebook-php-webdriver php-webdriver


    【解决方案1】:

    使用 cssSelector()selectByVisibleText() 函数,我现在可以选择我想要的任何选项。谢谢。

    with(new WebDriverSelect($driver->findElement(WebDriverBy::cssSelector('div[title="ExchangeEmail"] select'))))
            ->selectByVisibleText('Off (optional)');
    

    【讨论】:

      【解决方案2】:

      selectByVisibleText() 命令可用于使用标签文本从下拉字段中选择列表选项:

      例如,使用 CSS 选择器:

      $selectDiv = WebDriverBy::cssSelector('div[title="ExchangeEmail"] select');
      $selectElement = new WebDriverSelect($driver->findElement($selectDiv));
      $selectElement->selectByVisibleText('Off (optional)');
      

      示例,使用 XPath:

      $sel = $driver->findElements(WebDriverBy::xpath('//select'));
      $select = new WebDriverSelect($sel);
      $select->selectByVisibleText('Off (optional)');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        • 2018-08-25
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多