英文:
how to select li of ul in selenium webdriver using java
问题
以下是已翻译的内容:
我是新手使用selenium。我正在使用selenium webdriver。我想要从ul的li中选择一个值。我需要从列表项中选择一个国家。
这是HTML代码;
<div class="select-wrapper initialized">
<span class="caret">▼</span>
<input type="text" class="select-dropdown" readonly="true" data-activates="select-options-45625784-059c-3523-a00e-3ad824fad64a" value="-- Select --">
<ul id="select-options-45625784-059c-3523-a00e-3ad824fad64a" class="dropdown-content select-dropdown ">
<li class=""><span>-- Select --</span></li>
<li class=""><span>Afghanistan</span></li>
<li class=""><span>Canada</span></li>
</ul>
<select ng-if="!form.translate" id="countryCode" materializecss-select="" ng-model="model['countryCode']" ng-model-options="form.ngModelOptions" ng-disabled="form.readonly" sf-changed="form" class="initialized" schema-validate="form" ng-options="item.value as item.name group by item.group for item in form.titleMap" name="countryCode">
<option label="-- Select --" value="string:" selected="selected">-- Select --</option>
<option label="Afghanistan" value="string:AF">Afghanistan</option>
<option label="Canada" value="string:AF">Canada</option>
</select>
</div>
这是我的selectCountry函数。
By countryInput = By.xpath("//[@id=\"countryCode_inputfileddiv\"]/div/input");
public void selectCountry() {
WebElement dropdown = driver.findElement(countryInput);
dropdown.click();
List<WebElement> options = driver.findElements(By.id("select-options-b9a0d236-4008-830c-5319-9405f74b872f"));
for (WebElement option : options) {
String valueToSelect = option.getText().trim();
System.out.println(valueToSelect);
if (valueToSelect.equals("Canada")) {
continue;
}
option.click();
}
}
如何点击下拉框?
我尝试了上面的示例行,但出现错误:
元素当前不可见,因此可能无法进行交互操作 命令持续时间
英文:
I am new to selenium. I am working on selenium webdriver. i want to select a value from the li of ul. I need to select a country from list items.
Here is the HTML code;
<div class="select-wrapper initialized">
<span class="caret">▼</span>
<input type="text" class="select-dropdown" readonly="true" data-activates="select-options-45625784-059c-3523-a00e-3ad824fad64a" value="-- Select --">
<ul id="select-options-45625784-059c-3523-a00e-3ad824fad64a" class="dropdown-content select-dropdown ">
<li class=""><span>-- Select --</span>
</li><li class=""><span>Afghanistan</span></li>
</li><li class=""><span>Canada</span></li>
</ul>
<select ng-if="!form.translate" id="countryCode" materializecss-select="" ng-model="model['countryCode']" ng-model-options="form.ngModelOptions" ng-disabled="form.readonly" sf-changed="form" class="initialized" schema-validate="form" ng-options="item.value as item.name group by item.group for item in form.titleMap" name="countryCode">
<option label="-- Select --" value="string:" selected="selected">-- Select --</option>
<option label="Afghanistan" value="string:AF">Afghanistan</option>
<option label="Canada" value="string:AF">Canada</option>
</select>
</div>
Here is my selectCountry function.
By countryInput = By.xpath("//*[@id=\"countryCode_inputfileddiv\"]/div/input");
public void selectCountry() {
WebElement dropdown = driver.findElement(countryInput);
dropdown.click();
List<WebElement> options = driver.findElements(By.id("select-options-b9a0d236-4008-830c-5319-9405f74b872f"));
for (WebElement option : options) {
String valueToSelect = option.getText().trim();
System.out.println(valueToSelect);
if (valueToSelect.equals("Canada")) {
continue;
}
option.click();
}
}
How to click the drop down?
I tried with the above example lines but am getting error such as
> Element is not currently visible and so may not be interacted with Command duration
答案1
得分: 1
我在HTML中看到一个<select>
节点。你可以使用selenium的Select
类来处理/选择下拉框的值。参考下面的代码:
Select drpCountry = new Select(driver.findElement(By.id("countryCode")));
drpCountry.selectByVisibleText("Canada");
所需的导入:
import org.openqa.selenium.support.ui.Select;
英文:
I see a <select>
node in the HTML. You can use selenium's Select
class to handle/choose dropdown values. Refer the code below:
Select drpCountry = new Select(driver.findElement(By.id("countryCode")));
drpCountry.selectByVisibleText("Canada");
Imports required:
import org.openqa.selenium.support.ui.Select;
答案2
得分: 0
public void selectCountry() {
WebElement dropdown = driver.findElement(By.id("countryCode"));
Select select = new Select(dropdown);
select.selectByVisibleText("Afghanistan");
}
if we want to select Canada just pass Canada in selectByVisibleText() method
英文:
public void selectCountry() {
WebElement dropdown = driver.findElement(By.id("countryCode"));
Select select=new Select(dropdown);
select.selectByVisibleText("Afghanistan");
}
if we want to select Canada just pass Canada in selectByVisibleText() method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论