微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

下面给出的是一段代码,表示下拉列表.
我需要在此下拉列表中选择日期值,该字段由< 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 elm = new Select(driver.findElement(By.xpath(".//*[@id='type']/option[3]")));
  elm.selectByVisibleText("Date");

控制台显示

Element should have been “select” but was “option”

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

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

控制台显示

DEBUG Element is missing an accessible name: 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]")));

控制台显示

DEBUG Element is missing an accessible name: 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();

控制台显示

Exception in thread “main”
org.openqa.selenium.UnsupportedCommandException: POST
/session/a37a745a-e40c-45a9-9760-8e01b451a017/moveto did not match a
kNown command (WARNING: The server did not provide any stacktrace
@R_398_4045@ion)

我还添加了在使用这段代码间的“等待之间”.为了简单起见,这里没有包括它.

需要帮忙.

解决方法:

在您的第一个选择硒中,硒明确表示元素应为“选择”但为“选择”,这意味着此处您为选项提供了xpath,而仅希望选择了xpath.

不需要使用您提供的其他选项,只需使用以下第一个选项即可:-

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

或ByIndex

elm.selectByIndex(2);

或ByValue

elm.selectByValue("1");

如果不幸的是您的第一个选项不起作用,我希望您使用下面的第三个选项Using 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");

希望对您有帮助…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐