下面给出了一段代码,表示一个下拉菜单。
我需要在由 <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 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/