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

标签 javascript java selenium

下面给出了一段代码,表示一个下拉菜单。 我需要在由 <option value="1" label="Date">Date</option> 表示的下拉菜单中选择 Date

<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 选择此值
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)

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

需要帮助。

最佳答案

在您的第一个选项 selenium 中明确说明 Element should have been "select"but was "option",意味着您在这里提供了 xpath对于 option而只期待 xpath供选择。

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

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

ByIndex

elm.selectByIndex(2);

ByValue

elm.selectByValue("1");

如果不幸的是您的第一个选项不起作用,我希望您使用第三个选项使用 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 - 如何使用 Selenium 从下拉列表中选择一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38239278/

相关文章:

java - 我在 tomcat/conf/web.xml 和 tomcat/lib 中创建了 tomcat 过滤器。页面内容没有为请求加载。但是过滤器正在执行

java - 使用 selenium java 迭代多个页面的产品列表

javascript - HTML Canvas 单元测试

javascript - 用于多个选择选项下拉列表的本地存储

java - 从 Firebase (android) 获取数据时如何避免代码重复?

java - 如何在Android中使用SmsManager发送具有自定义线程ID的短信?

java - 守夜人 Selenium "socket hang up"

java - 如何使用 Selenium WebDriver + Java 获取浏览器实例的 PID?

javascript - 页面缩放时可使用哪些事件

javascript - 了解使用 JS Angular + HTML 进行导航的语法(ng-click ="save(userForm)".....)