【问题标题】:How To select a Value From Drop-Down using Selenium?如何使用 Selenium 从下拉列表中选择一个值?
【发布时间】:2016-11-09 09:45:26
【问题描述】:

下面是一段代码,它表示一个下拉菜单。 我需要在此下拉列表中选择 Date 值,由<option value="1" label="Date">Date</option> 表示

<select id="type" class="text-input ng-pristine ng-valid ng-scope ng-touched" ng-style="cssStyle" name="type" ng-if="!options.hidePlaceholder" ng-model="result.type" qmx-observe-value="text" ng-disabled="options.readonly" ng-options="obj.value as obj.text group by obj.groupby for obj in selectData" style="font-size: 13px; opacity: 1; width: 100%;">
    <option class="ng-binding" value="">----</option>
    <option value="0" selected="selected" label="Text">Text</option>
    <option value="1" label="Date">Date</option>
    <option value="2" label="Numeric">Numeric</option>
    <option value="3" label="Switch">Switch</option>
    <option value="4" label="Map Location Marker">Map Location Marker</option>
    <option value="5" label="Picker">Picker</option>
    <option value="6" label="List">List</option>
    </select>

以下方法无效。
1.) 通过导入 org.openqa.selenium.support.ui.Select

使用 Select 选择此值
Select elm = new Select(driver.findElement(By.xpath(".//*[@id='type']/option[3]")));
  elm.selectByVisibleText("Date");

控制台显示:

元素应该是“选择”但是“选项”


2.) 首先单击下拉菜单以显示要选择的选项,然后单击该选项。

driver.findElement(By.xpath(".//*[@id='type']")).click();
driver.findElement(By.xpath(".//*[@id='type']/option[3]")).click();

控制台显示:

DEBUG 元素缺少可访问的名称:id: type, tagName: 选择,类名:文本输入 ng-pristine ng-untouched ng-valid ng-范围


3.) 使用 JavascriptExecutor 获取点击。

driver.findElement(By.xpath(".//*[@id='type']")).click();    
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*[@id='type']/option[3]")));

控制台显示:

DEBUG 元素缺少可访问的名称:id: type, tagName: 选择,类名:文本输入 ng-pristine ng-untouched ng-valid ng-范围


4.) 使用鼠标悬停在下拉选项中被选中,然后执行点击它。

driver.findElement(By.xpath(".//*[@id='type']")).click();    
WebElement subdrop = driver.findElement(By.xpath(".//*[@id='type']/option[3]"));
        Actions action = new Actions(drive);
        action.moveToElement(subdrop).perform();
        Custom.Twait();
        action.click(subdrop).perform();

控制台显示:

线程“main”中的异常 org.openqa.selenium.UnsupportedCommandException: POST /session/a37a745a-e40c-45a9-9760-8e01b451a017/moveto 不匹配 已知命令(警告:服务器未提供任何堆栈跟踪 信息)

我还在使用此代码的位置添加了 Wait in Between。为了简单起见,我没有包括它。

需要帮助。

【问题讨论】:

    标签: javascript java selenium


    【解决方案1】:

    在您的第一个选项中硒明确表示 元素应该是“选择”但是“选项”,这意味着您在这里为option 提供xpath,而只为xpath 提供选择。

    不需要使用您提供的其他选项,只需使用您的第一个选项,如下所示:-

    Select elm = new Select(driver.findElement(By.id("type")));
    elm.selectByVisibleText("Date");
    

    ByIndex

    elm.selectByIndex(2);
    

    ByValue

    elm.selectByValue("1");
    

    如果您的第一个选项不幸不起作用,我希望您使用第三个选项使用JavascriptExecutor,如下所示:-

    WebElement select = driver.findElement(By.id("type"));
    
    ((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, "Date");
    

    希望对你有所帮助...:)

    【讨论】:

    • 它没有用。我目前正在使用sendKeys(keys.ARROW_DOWN),然后使用senKeys(keys.RETURN) 方法来选择似乎有效的选项,但我肯定想知道实现此目标的其他方法。
    • JavascriptExecutor 代码就像一个魅力。非常感谢。
    • @PawanJuyal 你能告诉我为什么第一个选项不起作用???有例外吗???
    • conn0 -> [0,25,"findElements",{"element":"c1efa248-5a65-43ac-af75-47bd7d306429","using":"xpath","value":" .//option[@value = \"2\"]"}] 1467883681756 conn0 [0,26,"i​​sElementSelected",{"id":"38f00c46-c8d1-4e43-ad5f-2d34ced5041 "}] 1467883681781 conn0 [0,27,"clickElement",{"id":"38f00c46-c8d1-4e43-ad5f-2d34d50441ce " 1467883681835 conn0
    • @PawanJuyal 你为什么要提供xPath.//option[@value = "2"]...你只需找到选择元素为By.id("type") 并将其提供给new Select() 以在selenium 中用作选择框...
    【解决方案2】:

    如果您在 Selenium - Python 中寻找解决方案: 使用 JS 调用。

    driver.execute_script("return document.getElementById('select id here').selectedIndex = '2'")
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多