javascript-如何使用硒从下拉列表中选择一个值?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何使用硒从下拉列表中选择一个值?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3230字,纯文字阅读大概需要5分钟。
内容图文
下面给出的是一段代码,表示下拉列表.
我需要在此下拉列表中选择日期值,该字段由< 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
information)
我还添加了在使用这段代码之间的“等待之间”.为了简单起见,这里没有包括它.
需要帮忙.
解决方法:
在您的第一个选择硒中,硒明确表示元素应为“选择”但为“选择”,这意味着此处您为选项提供了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");
希望对您有帮助…
内容总结
以上是互联网集市为您收集整理的javascript-如何使用硒从下拉列表中选择一个值?全部内容,希望文章能够帮你解决javascript-如何使用硒从下拉列表中选择一个值?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。