如何使用Selenium从下拉列表中选择值? [英] How To select a Value From Drop-Down using Selenium?

查看:3215
本文介绍了如何使用Selenium从下拉列表中选择值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下是一段代表下拉列表的代码。
我需要在此下拉列表中选择日期值,表示为< option value =1label =Date> Date< / option>

Given Below is a Piece of Code which denotes a Drop-Down. I need to Select Date value in this Drop-down denoted By <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.)点击Drop-首先显示要选择的选项,然后单击选项。


2.) Clicking on the Drop-Down first to display option to be selected and then clicking on the option.

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

控制台显示:


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

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


3.)使用JavascriptExecutor获取点击。


3.) Using JavascriptExecutor to get the click.

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:
SELECT,className:text-input ng-pristine ng-untouched ng-valid
ng-scope

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


4.)使用鼠标悬停选项在下拉菜单中选择,然后点击它。


4.) Using Mouse-Over on Option to be selected in Drop-down and then performing click on it.

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与已知的
不匹配命令(警告:服务器没有提供任何栈跟踪
信息)

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)

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

需要帮助。

推荐答案

在你的第一个选项中,selenium清楚地说元素应该是select但是option,这意味着你提供 xpath 用于选项,而期望只有 xpath 用于选择。

In your first option selenium clearly saying Element should have been "select" but was "option", means here you are providing the xpath for option while expecting only xpath for select.

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

Don't need to use other option as you provided, Just use your first option as below :-

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

ByIndex

elm.selectByIndex(2);

ByValue

elm.selectByValue("1");

如果您的第一个选项不幸无法运行,我希望您使用第三个选项使用 JavascriptExecutor 如下: -

If your first option unfortunatly not work I prefer you to use your third option Using JavascriptExecutor as below :-

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");

希望它能帮到你...... :)

Hope it will help you...:)

这篇关于如何使用Selenium从下拉列表中选择值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆