如何使用 Selenium 从下拉列表中选择一个值?

Paw*_*yal 3 javascript java selenium

下面给出的是一段代码,它表示一个下拉菜单。我需要在此下拉列表中选择日期值,表示为<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>
Run Code Online (Sandbox Code Playgroud)

以下方法不起作用。
1.)通过导入org.openqa.selenium.support.ui.Select使用Select选择此值

Select elm = new Select(driver.findElement(By.xpath(".//*[@id='type']/option[3]")));
  elm.selectByVisibleText("Date");
Run Code Online (Sandbox Code Playgroud)

控制台显示:

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


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

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

控制台显示:

DEBUG 元素缺少可访问的名称:id:type,tagName:SELECT,className:text-input ng-pristine ng-untouched ng-valid ng-scope


3.) 使用 JavascriptExecutor 获得点击。

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

控制台显示:

DEBUG 元素缺少可访问的名称:id:type,tagName:SELECT,className:text-input ng-pristine ng-untouched ng-valid ng-scope


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();
Run Code Online (Sandbox Code Playgroud)

控制台显示:

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

我还在使用此代码的地方添加了 Wait in between。这里为了简单起见,我没有包括它。

需要帮忙。

Sau*_*aur 5

在您的第一个选项 selenium 中,清楚地说明Element 应该是 "select" 但是是 "option",这意味着您在这里提供xpathforoption而只期望xpath选择。

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

Select elm = new Select(driver.findElement(By.id("type")));
elm.selectByVisibleText("Date");
Run Code Online (Sandbox Code Playgroud)

或者 ByIndex

elm.selectByIndex(2);
Run Code Online (Sandbox Code Playgroud)

或者 ByValue

elm.selectByValue("1");
Run Code Online (Sandbox Code Playgroud)

如果您的第一个选项不幸不起作用,我希望您使用第三个选项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");
Run Code Online (Sandbox Code Playgroud)

希望能帮到你...:)