简体   繁体   English

如何从 selenium (NodeJs) 的下拉菜单中选择 select 选项

[英]How to select option from dropdown in selenium (NodeJs)

I want to write a script for testing dropdown inside the webpage.我想编写一个脚本来测试网页内的下拉菜单。 But, I was unable to do so in selenium using Nodejs.但是,我无法使用 Nodejs 在 selenium 中这样做。 Is there any method to test dropdown in selenium(NodeJs).有什么方法可以测试 selenium(NodeJs)中的下拉菜单。

Thanks in advance提前致谢

You can create a select class like this and then use method based on your selection.您可以像这样创建 select class,然后根据您的选择使用方法。

var SelectWrapper = function(selector) {
    this.webElement = element(selector);
};
SelectWrapper.prototype.getOptions = function() {
    return this.webElement.all(by.tagName('option'));
};
SelectWrapper.prototype.getSelectedOptions = function() {
    return this.webElement.all(by.css('option[selected="selected"]'));
};
SelectWrapper.prototype.selectByValue = function(value) {
    return this.webElement.all(by.css('option[value="' + value + '"]')).click();
};
SelectWrapper.prototype.selectByPartialText = function(text) {
    return this.webElement.all(by.cssContainingText('option', text)).click();
};
SelectWrapper.prototype.selectByText = function(text) {
    return this.webElement.all(by.xpath('option[.="' + text + '"]')).click();
};

module.exports = SelectWrapper;

Usage in test class测试中的用法 class

var SelectWrapper = require('./select-wrapper.js');
var mySelect = new SelectWrapper(by.id("validSelectTagID"));

describe("Select Wrapper",function(){
    it("Handling the dropdown list",function(){
        browser.get("http://validURL");
        mySelect.selectByText("TextinDrop");
        mySelect.selectByValue("ValueInDrop");
    }) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从下拉列表中选择选项并使用nodejs提交请求 - Select option from dropdown and submit request using nodejs 如何从 Google Forms 弹出下拉列表中选择一个选项 Puppeteer NodeJS - How to Select an opion from a Google Forms popup dropdown Puppeteer NodeJS 如何使用NodeJS从MySql数据库中的数据创建html select选项 - How to create html select option with data from MySql database with nodeJS 如何使用NodeJS从MySql数据库中的数据创建html选择选项菜单 - How to create html select option menu with data from MySql database with nodeJS 从网站下拉列表中随机选择项目 - puppeteer、nodeJS - Select item from website dropdown at random - puppeteer, nodeJS 使用NodeJS数据库中的选项值选择表单 - Select form using option values from NodeJS database Puppeteer:如何根据文本选择下拉选项? - Puppeteer: How to select a dropdown option based on its text? 如何使用NodeJS获取MongoDB数据以选择选项 - How to get MongoDB data to select option using NodeJS 使用Dalekjs测试工具,当Option标记中没有“值”属性时,如何在Dropdown(选择元素)中选择Option? - Using Dalekjs test tool, how to select Option in Dropdown(Select element) when there is no “value” attribute in Option tag? 如何使用 node.js 在 selenium webdriver 中选择下拉值 - how to select a dropdown value in selenium webdriver using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM